对于使用 @RolesAllowed、@Secured 或 @PreAuthorize 的任何授权请求,Spring 引



我一直在研究这篇文章(以及其他一些类似的文章(: https://medium.com/omarelgabrys-blog/microservices-with-spring-boot-authentication-with-jwt-part-3-fafc9d7187e8

客户端是一个 Angular 8 应用程序,它从独立的微服务获取 JWT。 尝试将筛选器添加到其他微服务,以需要通过 jwt 角色进行特定授权。

持续收到 403 错误。

安全配置:

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true,
securedEnabled = true,
jsr250Enabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private BCryptPasswordEncoder bCryptPasswordEncoder;
public WebSecurityConfig() {}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and().csrf().disable()
// make sure we use stateless session; session won't be used to store user's state.
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
// Add a filter to validate the tokens with every request
.addFilterAfter(new JwtAuthorizationFilter2(), UsernamePasswordAuthenticationFilter.class)
// authorization requests config
.authorizeRequests()
// Any other request must be authenticated
.anyRequest().authenticated();
}
}

滤波器:

public class JwtAuthorizationFilter2 extends OncePerRequestFilter {
private final String HEADER = "Authorization";
private final String PREFIX = "Bearer ";
private final String SECRET = "foo";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
String token = request.getHeader(SecurityConstants.HEADER_STRING);
if (token != null) {
// parse the token.
DecodedJWT decoded = JWT.require(Algorithm.HMAC512(SecurityConstants.SECRET.getBytes()))
.build()
.verify(token.replace(SecurityConstants.TOKEN_PREFIX, ""));
String user = decoded.getSubject();
List<SimpleGrantedAuthority> sgas = Arrays.stream(
decoded.getClaim("roles").asArray(String.class))
.map( s -> new SimpleGrantedAuthority(s))
.collect( Collectors.toList());
if (sgas != null) {
sgas.add(new SimpleGrantedAuthority("FOO_Admin"));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
user,
null,
sgas);
SecurityContextHolder.getContext().setAuthentication(auth);
}
else {
SecurityContextHolder.clearContext();
}
chain.doFilter(request, response);
}
}
}

此代码无需定义任何授权要求即可正常工作,但如果在 WebSecurityConfig 中定义了授权,或者通过修饰控制器方法,则会为范围内的所有请求接收 http 403。

例子:

.authorizeRequests().antMatchers("/**").hasRole("FOO_Admin")
// or any of these 
@PreAuthorize("hasRole('FOO_Admin')")
@RolesAllowed({"FOO_Admin"})
@Secured({"FOO_Admin"})
Device get(@PathVariable String id) {
// some code
}

当代码在SecurityContextHolder.getContext().setAuthentication(auth)时停止时,auth.authenticated = trueauth.authorities包括用于"FOO_Admin"的 SimpleGrantedAuthority

所以我想知道是否: FilterChain 需要一个身份验证过滤器(或者身份验证是否发生在 JwtAuthorizationFilter2 中?(? 角色名称没有拼写、格式或大小写差异。

我愣住了。 任何帮助将不胜感激。

@PreAuthorize("hasRole('FOO_Admin'))期望用户具有权限ROLE_FOO_Admin,该权限将以ROLE_为前缀。但是,用户只有权限FOO_Admin,因此无法访问该方法。

您有以下几种选择:

(1( 通过声明 BeanGrantedAuthorityDefaults来更改前缀:

@Bean
GrantedAuthorityDefaults grantedAuthorityDefaults() {
return new GrantedAuthorityDefaults("FOO");
}

并使用@PreAuthorize(hasRole('Admin'))来保护该方法。

(2(或者更简单的是使用@PreAuthorize("hasAuthority('FOO_Admin')"),它会直接检查用户是否具有权限FOO_Admin,而不添加任何前缀。

P.SJwtAuthorizationFilter2仅验证用户是否有效,并获取相关用户信息,以便稍后为授权用户做准备。这是一种身份验证,我会将其重命名为JwtAuthenticationFilter2以更准确地描述它的实际功能。

相关内容

最新更新