如何使用SpringBoot Security禁用OPTIONS的基本http auth

时间:2017-05-24 09:19:33

标签: java spring spring-boot spring-security

我有一个小的休息服务,受默认的Spring Boot安全配置保护。默认情况下,它需要在每个http方法上进行授权,包括OPTIONS,chrome但是不会给飞行员提供飞行,并且不会在预检请求中包含授权标头,从而导致401响应。

如何在特定方法上禁用http basic auth?到目前为止我试过了:

@Configuration
@EnableWebMvc
@EnableGlobalMethodSecurity(securedEnabled = true)
public class Config {
}

在控制器中:

@CrossOrigin(origins = {"http://localhost:4200"}, maxAge = 4800)
@RestController
public class MainController {

    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    @RequestMapping(method = RequestMethod.OPTIONS)
    public ResponseEntity handle() {
        return new ResponseEntity(HttpStatus.OK);
    }
}

显然不起作用。

1 个答案:

答案 0 :(得分:1)

方法

@Override
protected void configure(HttpSecurity http) throws Exception
{

添加

.antMatchers(HttpMethod.OPTIONS, "/path/to/skip/check").permitAll()