@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.oauth2ResourceServer(oauth2Configurer -> oauth2Configurer.jwt(jwtConfigurer -> jwtConfigurer.jwtAuthenticationConverter(jwt -> {
Map<String, Collection<String>> realmAccess = jwt.getClaim("realm_access");
Collection<String> roles = realmAccess.get("roles");
var grantedAuthorities = roles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
.toList();
return new JwtAuthenticationToken(jwt, grantedAuthorities);
})))
;
httpSecurity.exceptionHandling(handleConfig -> handleConfig.accessDeniedHandler(m_AccessDeniedHandler));
return httpSecurity.build();
}
1.OAuth2ResourceServerConfigurer
核心功能包含:
- 创建JwtConfigurer实例。
- 创建JwtConfigurer实例中实例变量jwtAuthenticationConverter:Converter【Lambda表达式】类型。该Lambda表达式真正实现逻辑是从jwt toke解析得到realm_access属性中value,最终将authorities抽象为JwtAuthenticationToken实例返回。
public final class OAuth2ResourceServerConfigurer{
private JwtConfigurer jwtConfigurer;
private BearerTokenRequestMatcher requestMatcher = new BearerTokenRequestMatcher();
public OAuth2ResourceServerConfigurer<H> jwt(Customizer<JwtConfigurer> jwtCustomizer) {
if (this.jwtConfigurer == null) {
this.jwtConfigurer = new JwtConfigurer(this.context);
}
// JwtConfigurer#jwtAuthenticationConverter
jwtCustomizer.customize(this.jwtConfig

最低0.47元/天 解锁文章
3631

被折叠的 条评论
为什么被折叠?



