Skip to content

Instantly share code, notes, and snippets.

@baso53
Last active November 14, 2021 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baso53/ded1048a7257b477cd3dd0c18863f0bc to your computer and use it in GitHub Desktop.
Save baso53/ded1048a7257b477cd3dd0c18863f0bc to your computer and use it in GitHub Desktop.
@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