我使用spring-security-oauth2-authorization-server(v.0.2.0)来实现我的授权服务器。我希望用户角色在令牌中,可以添加它们吗?谢谢
t3psigkw1#
我通过添加以下bean解决了这个问题
@Bean OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() { return context -> { if (context.getTokenType() == OAuth2TokenType.ACCESS_TOKEN.getValue()) { Authentication principal = context.getPrincipal(); Set<String> authorities = principal.getAuthorities().stream() .map(GrantedAuthority::getAuthority) .collect(Collectors.toSet()); context.getClaims().claim("user-authorities", authorities); } }; }
ruoxqz4g2#
它应该是'=='检查,ACCESS_TOKEN不是值。
@Bean OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() { return context -> { if (context.getTokenType() == OAuth2TokenType.ACCESS_TOKEN) { Authentication principal = context.getPrincipal(); Set<String> authorities = principal.getAuthorities().stream() .map(GrantedAuthority::getAuthority) .collect(Collectors.toSet()); context.getClaims().claim("user-authorities", authorities); } }; }
2条答案
按热度按时间t3psigkw1#
我通过添加以下bean解决了这个问题
ruoxqz4g2#
它应该是'=='检查,ACCESS_TOKEN不是值。