Last active
November 14, 2021 12:57
-
-
Save baso53/ded1048a7257b477cd3dd0c18863f0bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) | |
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http.csrf().disable(); | |
http.oauth2ResourceServer() | |
.jwt() | |
.jwtAuthenticationConverter(jwtAuthenticationConverter()); | |
} | |
public JwtAuthenticationConverter jwtAuthenticationConverter() { | |
JwtAuthenticationConverter converter = new JwtAuthenticationConverter(); | |
converter.setJwtGrantedAuthoritiesConverter(jwt -> | |
Optional.ofNullable(jwt.getClaimAsStringList("custom_claims")) | |
.stream() | |
.flatMap(Collection::stream) | |
.flatMap(claim -> { | |
var parts = claim.split(":", 3); | |
EntityType entityType; | |
Long entityId; | |
Permission permission; | |
try { | |
entityType = EntityType.valueOf(parts[0]); | |
entityId = Long.valueOf(parts[1]); | |
permission = Permission.valueOf(parts[2]); | |
} catch (IllegalArgumentException e) { | |
return Stream.empty(); | |
} | |
return Stream.of(new DomainGrantedAuthority(entityType, entityId, permission)); | |
}) | |
.collect(Collectors.toList())); | |
return converter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment