Search in sources :

Example 16 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities.

@Test
public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("ROLE_message:read"), new SimpleGrantedAuthority("ROLE_message:write"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)16 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)16 Jwt (org.springframework.security.oauth2.jwt.Jwt)16