【问题标题】:How to disable basic http auth for OPTIONS with SpringBoot Security如何使用 SpringBoot Security 禁用 OPTIONS 的基本 http auth
【发布时间】:2017-10-24 13:10:24
【问题描述】:

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

如何在特定方法上禁用 http 基本身份验证?到目前为止,我尝试过:

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

显然没有工作。

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    在方法中

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

    添加

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

    【讨论】:

    • 非常感谢,这有效。不得不寻找采用 HttpMethod 的 .antMatchers 方法。我的最终解决方案如下所示:http.httpBasic().and().authorizeRequests().antMatchers(HttpMethod.OPTIONS).permitAll();
    • 这不适用于具有 x509 客户端证书身份验证的双向 SSL 配置。
    • 确保将http对象上的函数调用顺序放在http对象上所有调用的开头,因为这些规则的顺序很重要,而permitAll()规则应该倾向于先走。
    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 2014-08-28
    • 2013-07-20
    • 2013-02-11
    • 2014-11-25
    • 2012-01-28
    • 2015-04-23
    • 2017-06-03
    相关资源
    最近更新 更多