org.springframework.security.core.authority.mapping.SimpleAuthorityMapper Java Examples

The following examples show how to use org.springframework.security.core.authority.mapping.SimpleAuthorityMapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LdapAuthenticationProviderConfigurer.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
private LdapAuthenticationProvider build() throws Exception {
    BaseLdapPathContextSource contextSource = getContextSource();
    LdapAuthenticator ldapAuthenticator = createLdapAuthenticator(contextSource);

    LdapAuthoritiesPopulator authoritiesPopulator = getLdapAuthoritiesPopulator();

    LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProviderProxy(
            ldapAuthenticator, authoritiesPopulator);
    SimpleAuthorityMapper simpleAuthorityMapper = new SimpleAuthorityMapper();
    simpleAuthorityMapper.setPrefix(rolePrefix);
    simpleAuthorityMapper.afterPropertiesSet();
    ldapAuthenticationProvider.setAuthoritiesMapper(simpleAuthorityMapper);
    if (userDetailsContextMapper != null) {
        ldapAuthenticationProvider
                .setUserDetailsContextMapper(userDetailsContextMapper);
    }
    return ldapAuthenticationProvider;
}
 
Example #2
Source File: SecurityConfig.java    From devconf2019-authz with Apache License 2.0 5 votes vote down vote up
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    SimpleAuthorityMapper grantedAuthorityMapper = new SimpleAuthorityMapper();
    grantedAuthorityMapper.setPrefix("ROLE_");
    grantedAuthorityMapper.setConvertToUpperCase(true);
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(grantedAuthorityMapper);
    auth.authenticationProvider(keycloakAuthenticationProvider);
}
 
Example #3
Source File: PersonApplication.java    From blog with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the KeycloakAuthenticationProvider with the authentication manager.
 */
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();

    // adding proper authority mapper for prefixing role with "ROLE_"
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

    auth.authenticationProvider(keycloakAuthenticationProvider);
}
 
Example #4
Source File: PersonApplication.java    From blog with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the KeycloakAuthenticationProvider with the authentication manager.
 */
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();

    // adding proper authority mapper for prefixing role with "ROLE_"
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

    auth.authenticationProvider(keycloakAuthenticationProvider);
}
 
Example #5
Source File: OidcUserManagementAutoConfiguration.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return a jwt authorities extractor which interprets the roles of a user
 *         as their authorities.
 */
@Bean
@ConditionalOnMissingBean
public JwtAuthoritiesExtractor jwtAuthoritiesExtractor() {
    final SimpleAuthorityMapper authorityMapper = new SimpleAuthorityMapper();
    authorityMapper.setPrefix("");
    authorityMapper.setConvertToUpperCase(true);

    return new JwtAuthoritiesExtractor(authorityMapper);
}
 
Example #6
Source File: KeycloakSpringAdapterUtilsTest.java    From smartling-keycloak-extras with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {

    MockitoAnnotations.initMocks(this);
    SimpleAuthorityMapper roleMapper = new SimpleAuthorityMapper();
    roleMapper.setConvertToUpperCase(true);
    grantedAuthoritiesMapper = roleMapper;

    PowerMockito.mockStatic(AdapterUtils.class);
    when(AdapterUtils.getRolesFromSecurityContext(any(RefreshableKeycloakSecurityContext.class))).thenReturn(AUTHORITIES);
    when(AdapterUtils.createPrincipal(eq(deployment), eq(context))).thenReturn(principal);
}
 
Example #7
Source File: KeycloakAuthenticationProviderTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testGrantedAuthoritiesMapper() throws Exception {
    SimpleAuthorityMapper grantedAuthorityMapper = new SimpleAuthorityMapper();
    grantedAuthorityMapper.setPrefix("ROLE_");
    grantedAuthorityMapper.setConvertToUpperCase(true);
    provider.setGrantedAuthoritiesMapper(grantedAuthorityMapper);

    Authentication result = provider.authenticate(token);
    assertEquals(Sets.newSet("ROLE_USER", "ROLE_ADMIN"),
        AuthorityUtils.authorityListToSet(result.getAuthorities()));
}
 
Example #8
Source File: SecurityConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
    auth.authenticationProvider(keycloakAuthenticationProvider);
}