【问题标题】:Use RequestHeaderRequestMatcher and antmactcher basic auth in the same configuration Spring Security在同一配置 Spring Security 中使用 RequestHeaderRequestMatcher 和 antmactcher 基本身份验证
【发布时间】:2020-06-05 17:48:44
【问题描述】:

我有这个过滤器:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers(new RequestHeaderRequestMatcher("Caller", "Rem"));
        // add here a seconde filter condition for basic Auth
    }

在标题过滤器之后,我想在相同的配置中使用下面的标识符 inMemory 创建另一个过滤器:

 @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .passwordEncoder(NoOpPasswordEncoder.getInstance())
            .withUser("myAccount").password("MyPassword").roles("USER");
    }

非常感谢。

【问题讨论】:

    标签: java spring-boot spring-security basic-authentication request-headers


    【解决方案1】:
    http.httpBasic().and()
                    .authorizeRequests()
                    .requestMatchers(new AndRequestMatcher(new RequestHeaderRequestMatcher("Caller", "Rem"), new AntPathRequestMatcher("/hello/one")))
                    .hasRole("USER")
                    .and()
                    .authorizeRequests().anyRequest().authenticated()
            .anyRequest().denyAll();
    

    在您的场景中将“/hello/one”替换为“/**”。

    解释:

    1. 如果请求中的路径与“/hello/one”不匹配,则服务器将返回 403。
    2. 如果路径匹配,它将检查身份验证,如果失败将返回 401,否则将进入下一步。
    3. 如果路径匹配且身份验证成功但标头不包含值为“Rem”的“Caller”,则将返回 403。
    4. 如果路径匹配,身份验证成功并且标头具有所需的键值,则将执行端点。

    【讨论】:

    • 我编辑了我的帖子,如果你能看到它,请提前谢谢
    猜你喜欢
    • 2014-11-21
    • 2014-10-27
    • 2016-06-08
    • 2011-02-11
    • 2016-03-20
    • 1970-01-01
    • 2015-02-06
    • 2015-03-27
    • 2015-10-07
    相关资源
    最近更新 更多