【问题标题】:Spring Security oauth and turn off security for some urls but keep basic auth activatedSpring Security oauth 并关闭某些 url 的安全性,但保持基本身份验证处于激活状态
【发布时间】:2016-11-02 14:00:12
【问题描述】:

我有一个使用 spring security oauth2 的项目。

HttpSecurity 配置如下所示:

http.antMatcher("/**").authorizeRequests().antMatchers(RESOURCE_LOCATIONS).permitAll().and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and().httpBasic().disable()
            .authorizeRequests().anyRequest().authenticated();

通过此设置,除 RESOURCE_LOCATIONS 下的 url 之外的任何请求都受 ouath2 协议保护。对于这些请求,HTTP Basic 已关闭。

现在我想保护 /internal/** 下面的网址,但只能通过 HTTP 基本身份验证。

我正在尝试类似的方法或类似的方法:

http.antMatcher("/**").authorizeRequests().antMatchers(RESOURCE_LOCATIONS).permitAll().and()
            .antMatcher("/internal/**").httpBasic().and() //<--ADDED THIS LINE
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and().httpBasic().disable()
            .authorizeRequests().anyRequest().authenticated();

但令人惊讶的是,所有请求都是公开的并且没有基本身份验证。

有没有人提示解决这个问题?

BR, 迈克尔

【问题讨论】:

    标签: java spring security spring-security


    【解决方案1】:

    嗯,

    如果你想让 url /internal/** 得到保护,更好的形式是使用 hasRole('') 和 httpBasic。您的应用程序需要通过简单的 HTTP 基本身份验证进行身份验证。但其他网页不应该使用 HTTP 基本,而是使用普通表单登录。因此,您可以创建一个 WebSecurity 只是为了 url 'Internal',如下所示。我希望这可以帮助你!

    @Configuration
    @Order(1)
    public static class InternalWebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .antMatcher("/internal/**")
                    .authorizeRequests()
                        .anyRequest().hasAnyRole("ADMIN", "USER")
                        .and()
                    .httpBasic();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2015-08-08
      • 2020-09-16
      • 2011-02-11
      • 2016-03-20
      • 1970-01-01
      • 2017-04-28
      • 2017-07-08
      • 2018-09-07
      相关资源
      最近更新 更多