Reputation: 1227
I'm trying to secure my website using Spring security following the guides on the web. I don't want my users to use my application through web browsers, so I disabled the csrf protection. The source code on the server side:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
implements ApplicationContextAware {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated().and()
.httpBasic().and()
.csrf().disable();
}
@Override
protected void registerAuthentication(AuthenticationManagerBuilde r authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication()
.withUser("user").password("password").roles("ADMI N");
}
}
@Controller
//@RequestMapping("/course")
public class CourseController implements ApplicationContextAware{
@RequestMapping(value="/course", method = RequestMethod.GET, produces="application/json")
public @ResponseBody List<Course> get(// The critirion used to find.
@RequestParam(value="what", required=true) String what,
@RequestParam(value="value", required=true) String value) {
//.....
}
@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public List<Course> upload(@RequestBody Course[] cs) {
}
}
I am using RestTemplate on the client side. The problem is that when I use POST method, I got warinning on the server side: o.s.web.servlet.PageNotFound : Request method 'POST' not supported
And on the client side, I got: Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
It is odd I got this exception because I already have the POST method handled in the Controller. Currently, the system still works, but this issue bothers me. Any idea? Thanks.
Upvotes: 6
Views: 38930
Reputation: 1227
Finally, I found what was wrong. Hope this helpful for someone else. This mistake is quite stupid. The return value of upload() should use @ResponseBody, because I want to return the courses directly.
@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public @ResponseBody List<Course> upload(@RequestBody Course[] cs) {
}
Upvotes: 6
Reputation: 3481
Not sure but try with setting both methods like this:
@RequestMapping(value="/course", method = { RequestMethod.GET, RequestMethod.POST } produces="application/json")
public @ResponseBody List<Course> get(// The critirion used to find.
@RequestParam(value="what", required=true) String what,
@RequestParam(value="value", required=true) String value) {
//.....
}
@RequestMapping(value="/course", method = { RequestMethod.GET, RequestMethod.POST } , produces="application/json")
public List<Course> upload(@RequestBody Course[] cs) {
}
Upvotes: 0