当我在Postman中尝试使用PutMapping
API时,出现“Invalid CORS请求”的错误提示。但是对于“POST”和“GET”映射,则正常工作。
为什么“PUT”操作不起作用?
我的Spring Boot版本是2.0。
这是我的配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/h2-console/**/**").permitAll()
.antMatchers(HttpMethod.GET,"/user/get-request").permitAll()
.antMatchers(HttpMethod.POST,"/user/post-request").permitAll()
.antMatchers(HttpMethod.PUT,"/user/put-request").permitAll()
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUserDetailService));
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").exposedHeaders("Authorization");
}
};
}
这是我的控制器:
@RestController
@RequestMapping("/user")
public class UserController {
@PutMapping("/put-request")
public void doResetPassword(@RequestBody String password) {
System.out.println("PUT MAPPING");
}
@PostMapping("/post-request")
public void doResetPassword(@RequestBody String password) {
System.out.println("POST MAPPING");
}
@GetMapping("/get-request")
public void doResetPassword() {
System.out.println("GET MAPPING");
}
}
.allowedMethods("*")
- Amir Azizkhani