5

我正在尝试按照网络上的指南使用 Spring 安全性来保护我的网站。我不希望我的用户通过网络浏览器使用我的应用程序,所以我禁用了 csrf 保护。服务端源码:

    @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) {
}
}

我在客户端使用 RestTemplate。问题是当我使用 POST 方法时,我在服务器端收到警告:osweb.servlet.PageNotFound : Request method 'POST' not supported

在客户端,我得到:线程“main”中的异常 org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed

奇怪的是我得到了这个异常,因为我已经在控制器中处理了 POST 方法。目前,该系统仍然有效,但这个问题困扰着我。任何想法?谢谢。

4

2 回答 2

6

最后,我发现了问题所在。希望这对其他人有帮助。这个错误非常愚蠢。upload() 的返回值应该使用@ResponseBody,因为我想直接返回课程。

@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public @ResponseBody List<Course> upload(@RequestBody Course[] cs) {
}
于 2013-10-27T14:11:21.703 回答
0

不确定,但尝试设置这两种方法:

@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) {
}
于 2013-10-22T08:25:01.460 回答