我正在尝试按照网络上的指南使用 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 方法。目前,该系统仍然有效,但这个问题困扰着我。任何想法?谢谢。