【问题标题】:Use Spring Security Filter to lock down everything except a few routes使用 Spring Security Filter 锁定除少数路由之外的所有内容
【发布时间】:2015-04-08 18:33:17
【问题描述】:

我们正在重新设计我们的产品以删除 SpringSecurity 中默认的“anonymousUser”行为,并希望锁定除少数端点之外的所有 URL(通过过滤器安全性)。我们想不通的是如何指定“锁定除 X、Y 和 Z 之外的所有内容”

我们的安全设置基本上归结为以下几点:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // disable anonymous users
            .anonymous().disable()

            // don't add ROLE_ to the role...
            .authorizeRequests()
                .regexMatchers("^/", "^/login", "^/mobile/login", "^/api/auth/.*")
                    .authenticated()
                .and()  
        ;
    }
}

我采取的其他路线类似于:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // disable anonymous users
            .anonymous().disable()

            // don't add ROLE_ to the role...
            .authorizeRequests()
            .antMatchers("/**")
                .authenticated()
            .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**")
                .permitAll()
            .and()
        ;
    }
}

任何建议/意见将不胜感激。

谢谢!

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    我们正在重新设计我们的产品以删除默认的“anonymousUser” SpringSecurity 中的行为

    我想知道你的意思是什么。根据其余描述,我认为您不需要以下内容(即您应该删除它):

    anonymous().disabled()
    

    上面说如果没有用户被认证,用户就会为null,这往往会导致NullPointerExceptions

    请记住,对于 authorizeRequests()(或拦截 URL),排序很重要。您拥有的 java 配置(针对readability 稍微重新格式化)

    .authorizeRequests()
        .antMatchers("/**").authenticated()
        .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**").permitAll()
        .and()
    

    将使用以下逻辑:

    • 此请求是否匹配“/**”?
      • 是的,所有内容都匹配“/**”。因此,每个请求都需要对用户进行身份验证。
    • 忽略所有其他规则,因为我们已经匹配

    您应该使用以下内容:

    .authorizeRequests()
        .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**").permitAll()
        .anyRequest().authenticated()
        .and()
    
    • 请求是否匹配“/”、“/login”或...?
      • 如果是,那么任何人都可以访问它并停止(不再使用规则)。
      • 如果请求不匹配,则继续。
    • 请求是否匹配任何请求?
      • 是的,所以如果请求与之前的规则不匹配,则需要对用户进行身份验证。

    注意:antMatchers("/**") 更简洁地表示为 anyRequest()

    【讨论】:

    • 这成功了。谢谢罗布。我没有完全理解的部分是anonymousUser 位的重要性。事实上,我得到的 NPE 被吞下/翻译为身份验证失败。
    • 使用matcher(...).permitAll().anyRequest().authenticated() 如果我没有通过授权标头(它实际上并没有验证标头),我仍然会得到 401。这是预期的吗?
    • 答案听起来合乎逻辑,但我也继续在路径上获得 401,这应该是允许的。 .authorizeRequests() .antMatchers("/", "/api/swagger-ui", "/api/swagger-ui/**").permitAll() .anyRequest().authenticated() .and()
    【解决方案2】:

    Rob Winch 的答案几乎在所有情况下都是正确的答案,也是我在项目中采用的方法。我确实认为还值得注意的是,另一种可能的方法是执行以下操作:

    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/assets/**", "/index.html");
    }
    

    请注意,这是与您之前提交的示例中的方法不同的方法。该方法有一个HttpSecurity 类型的参数,而这个是WebSecurity 类型的参数

    该代码示例的作用是找到所有匹配的请求并一起完全跳过 http 安全过滤器。因此,如果您想优化一些您知道需要零功能的请求 HttpSecurity 提供那么这可能是一个很好的解决方案。这意味着,如果您使用 csrf()requestCache()headers() 等功能,它们将不会应用于上述示例中的匹配请求(“/assets/**”、“/index.html”)

    【讨论】:

      【解决方案3】:

      如果你使用 security.xml 我知道你可以做这样的事情

      <http use-expressions="true" entry-point-ref="authenticationEntryPoint">
          <custom-filter position="BASIC_AUTH_FILTER" ref="loginFilter" />
          <intercept-url pattern="/login" access="permitAll" />
          <intercept-url pattern="/mobile/login" access="permitAll" />
          <intercept-url pattern="/api/auth/**" access="fullyAuthenticated" />
          <intercept-url pattern="/**" access="fullyAuthenticated" />
          <logout success-handler-ref="logoutHandler" />
      </http>
      

      它的作用是允许任何人访问登录或移动/登录,但只允许完全通过身份验证的人访问 api/auth 或网站上的任何其他内容

      <intercept-url pattern="/api/auth/**" access="fullyAuthenticated" />
      

      可以删除此行,并且可以使用相同的功能。它从上到下开始,因此顶部允许的任何内容优先。

      【讨论】:

      • 我刚刚用第二个代码块更新了我的问题,它在 JavaConfig 中基本上是等效的,但仍然没有爱。我也尝试过切换顺序,但没有成功。想法?并感谢您的意见:)
      猜你喜欢
      • 2012-11-03
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 2013-08-31
      • 2019-07-16
      • 1970-01-01
      • 2012-09-05
      相关资源
      最近更新 更多