Код: Выделить всё
@RequestMapping(value = "/signup", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody Message signUp(@RequestBody Message message) {
logger.info("Accessing protected resource");
return new Message(100, "Congratulations!", "You have signed up. msg:"+message.toString());
}
Код: Выделить всё
@Override
protected Message doInBackground(Void... params) {
// TODO Auto-generated method stub
final String url = "http://10.0.2.2:8080/signup";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity requestEntity = new HttpEntity(signupmsg, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
try {
// Make the network request
Log.d(TAG, url);
ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Message.class);
return response.getBody();
} catch (HttpClientErrorException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
return new Message(0, e.getStatusText(), e.getLocalizedMessage());
} catch (ResourceAccessException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
return new Message(0, e.getClass().getSimpleName(), e.getLocalizedMessage());
}
}
Объект Message определяется пользователем в отдельном классе. Однако если я отправлю запрос GET без инкапсулированного объекта, он сработает. Я новичок в Spring. Что мне здесь не хватает?
Обновление: Я диагностировал эту проблему, и она возникает, когда я включаю Spring Web Security. При отсутствии безопасности запрос POST выполняется успешно. Я попробовал отключить безопасность в конфигурации, но все равно не работает. (Однако запросы GET работают нормально.) В настоящее время в моем WebSecurityConfiguration.java есть следующее
Код: Выделить всё
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth.inMemoryAuthentication()
.withUser("roy")
.password("spring")
.roles("USER");
// @formatter:on
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.anyRequest().permitAll()
.and()
.httpBasic().disable();
// @formatter:on
}
Код: Выделить всё
compile("org.springframework.boot:spring-boot-starter-security")Состояние HTTP 403 — ожидаемый токен CSRF не найден. Срок действия вашего сеанса истек?
Это временно исправлено с помощью .csrf().disable()
Как лучше всего решить эту проблему?
Подробнее здесь: https://stackoverflow.com/questions/273 ... -forbidden
Мобильная версия