【发布时间】: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
是否应该通过向路由添加角色约束来修复这种预期行为?
【问题讨论】:
-
authenticated和fullyAuthenticated之间存在差异。你也确定你的antmatchers是正确的吗?因为/api/*只会匹配一个级别,所以/api/auth/whatever不会被检测到(因为那是2级深)。此外,您的订单已关闭,因为声明匹配器的顺序也是咨询它们的顺序。因此,如果您真的想要/api/**(所有级别),它应该始终是最后一个。 -
是的。盲目地从另一个项目中复制东西。你在所有方面都是对的。这是通配符。订购也应该反过来......
标签: spring spring-boot spring-security