【问题标题】:Spring Filter chain - antMatchers(...).authenticated() not being enforcedSpring 过滤器链 - antMatchers(...).authenticated() 未强制执行
【发布时间】:2020-08-04 02:56:36
【问题描述】:

我有一个使用自定义 Jwt 令牌实现的应用程序。 身份验证部分工作得很好,令牌被创建/验证得很好。我的安全配置如下所示:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class DJWTSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    JwtTokenProvider jwtTokenProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().disable()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/api/*").authenticated()
                .antMatchers("/api/auth/signin").permitAll()
                .and()
                .apply(new JwtConfigurer(jwtTokenProvider));
    }
}

出于某种原因,没有对 api 请求强制执行安全性。对于没有不记名标头的请求,Spring 似乎认为“匿名”用户是经过身份验证的。检查安全上下文:

org.springframework.security.authentication.AnonymousAuthenticationToken@f27f7551: 
Principal: anonymousUser; 
Credentials: [PROTECTED]; 
Authenticated: true; 
Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; 
SessionId: null; 
Granted Authorities: ROLE_ANONYMOUS

是否应该通过向路由添加角色约束来修复这种预期行为?

【问题讨论】:

  • authenticatedfullyAuthenticated 之间存在差异。你也确定你的antmatchers是正确的吗?因为/api/* 只会匹配一个级别,所以/api/auth/whatever 不会被检测到(因为那是2级深)。此外,您的订单已关闭,因为声明匹配器的顺序也是咨询它们的顺序。因此,如果您真的想要/api/**(所有级别),它应该始终是最后一个。
  • 是的。盲目地从另一个项目中复制东西。你在所有方面都是对的。这是通配符。订购也应该反过来......

标签: spring spring-boot spring-security


【解决方案1】:
  • 通配符错误 - 应该是 /api/***
  • 与给定的错误无关,但顺序应该相反

【讨论】:

    猜你喜欢
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2023-03-12
    • 2014-12-12
    • 2013-01-05
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多