org.springframework.security.oauth2.core.oidc.OidcUserInfo Java Examples
The following examples show how to use
org.springframework.security.oauth2.core.oidc.OidcUserInfo.
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: SecurityConfiguration.java From java-microservices-examples with Apache License 2.0 | 7 votes |
/** * Map authorities from "groups" or "roles" claim in ID Token. * * @return a {@link GrantedAuthoritiesMapper} that maps groups from * the IdP to Spring Security Authorities. */ @Bean @SuppressWarnings("unchecked") public GrantedAuthoritiesMapper userAuthoritiesMapper() { return (authorities) -> { Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); authorities.forEach(authority -> { OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority; OidcUserInfo userInfo = oidcUserAuthority.getUserInfo(); Collection<String> groups = (Collection<String>) userInfo.getClaims().get("groups"); if (groups == null) { groups = (Collection<String>) userInfo.getClaims().get("roles"); } mappedAuthorities.addAll(groups.stream() .filter(group -> group.startsWith("ROLE_")) .map(SimpleGrantedAuthority::new).collect(Collectors.toList())); }); return mappedAuthorities; }; }
Example #2
Source File: SecurityConfiguration.java From java-microservices-examples with Apache License 2.0 | 7 votes |
/** * Map authorities from "groups" or "roles" claim in ID Token. * * @return a {@link GrantedAuthoritiesMapper} that maps groups from * the IdP to Spring Security Authorities. */ @Bean @SuppressWarnings("unchecked") public GrantedAuthoritiesMapper userAuthoritiesMapper() { return (authorities) -> { Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); authorities.forEach(authority -> { OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority; OidcUserInfo userInfo = oidcUserAuthority.getUserInfo(); Collection<String> groups = (Collection<String>) userInfo.getClaims().get("groups"); if (groups == null) { groups = (Collection<String>) userInfo.getClaims().get("roles"); } mappedAuthorities.addAll(groups.stream() .filter(group -> group.startsWith("ROLE_")) .map(SimpleGrantedAuthority::new).collect(Collectors.toList())); }); return mappedAuthorities; }; }
Example #3
Source File: SecurityConfiguration.java From java-microservices-examples with Apache License 2.0 | 6 votes |
/** * Map authorities from "groups" or "roles" claim in ID Token. * * @return a {@link GrantedAuthoritiesMapper} that maps groups from * the IdP to Spring Security Authorities. */ @Bean @SuppressWarnings("unchecked") public GrantedAuthoritiesMapper userAuthoritiesMapper() { return (authorities) -> { Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); authorities.forEach(authority -> { OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority; OidcUserInfo userInfo = oidcUserAuthority.getUserInfo(); Collection<String> groups = (Collection<String>) userInfo.getClaims().get("groups"); if (groups == null) { groups = (Collection<String>) userInfo.getClaims().get("roles"); } mappedAuthorities.addAll(groups.stream() .filter(group -> group.startsWith("ROLE_")) .map(SimpleGrantedAuthority::new).collect(Collectors.toList())); }); return mappedAuthorities; }; }
Example #4
Source File: SecurityConfig.java From oauth2-client with MIT License | 5 votes |
/** * 从user-info-uri 返回结果中抽取权限信息,如角色等,默认为scope * Mapping User Authorities * https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2login-advanced-map-authorities */ @Deprecated private GrantedAuthoritiesMapper userAuthoritiesMapper() { return (authorities) -> { Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); authorities.forEach(authority -> { if (OidcUserAuthority.class.isInstance(authority)) { OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority; OidcIdToken idToken = oidcUserAuthority.getIdToken(); OidcUserInfo userInfo = oidcUserAuthority.getUserInfo(); System.out.println(oidcUserAuthority); // Map the claims found in idToken and/or userInfo // to one or more GrantedAuthority's and add it to mappedAuthorities } else if (OAuth2UserAuthority.class.isInstance(authority)) { OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority) authority; Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes(); System.out.println(userAttributes); // Map the attributes found in userAttributes // to one or more GrantedAuthority's and add it to mappedAuthorities } else if (SimpleGrantedAuthority.class.isInstance(authority)) { SimpleGrantedAuthority simpleGrantedAuthority = (SimpleGrantedAuthority) authority; System.out.println(simpleGrantedAuthority); } }); return mappedAuthorities; }; }
Example #5
Source File: OAuth2SecurityConfiguration.java From jhipster-registry with Apache License 2.0 | 5 votes |
@Bean @SuppressWarnings("unchecked") public GrantedAuthoritiesMapper userAuthoritiesMapper() { return (authorities) -> { Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); authorities.forEach(authority -> { OidcUserInfo userInfo = null; // Check for OidcUserAuthority because Spring Security 5.2 returns // each scope as a GrantedAuthority, which we don't care about. if (authority instanceof OidcUserAuthority) { OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority; userInfo = oidcUserAuthority.getUserInfo(); } if (userInfo == null) { mappedAuthorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER)); } else { Map<String, Object> claims = userInfo.getClaims(); Collection<String> groups = (Collection<String>) claims.getOrDefault("groups", claims.getOrDefault("roles", new ArrayList<>())); mappedAuthorities.addAll(groups.stream() .filter(group -> group.startsWith("ROLE_")) .map(SimpleGrantedAuthority::new) .collect(toList())); } }); return mappedAuthorities; }; }
Example #6
Source File: OidcUserManagementAutoConfiguration.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
@Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication instanceof JwtAuthenticationToken) { final String defaultTenant = "DEFAULT"; final JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication; final Jwt jwt = jwtAuthenticationToken.getToken(); final OidcIdToken idToken = new OidcIdToken(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaims()); final OidcUserInfo userInfo = new OidcUserInfo(jwt.getClaims()); final Set<GrantedAuthority> authorities = authoritiesExtractor.extract(clientRegistration.getClientId(), jwt.getClaims()); if (authorities.isEmpty()) { ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN); return; } final DefaultOidcUser user = new DefaultOidcUser(authorities, idToken, userInfo); final OAuth2AuthenticationToken oAuth2AuthenticationToken = new OAuth2AuthenticationToken(user, authorities, clientRegistration.getRegistrationId()); oAuth2AuthenticationToken.setDetails(new TenantAwareAuthenticationDetails(defaultTenant, false)); systemSecurityContext.runAsSystemAsTenant(systemManagement::getTenantMetadata, defaultTenant); SecurityContextHolder.getContext().setAuthentication(oAuth2AuthenticationToken); } chain.doFilter(request, response); }
Example #7
Source File: MappedOidcUser.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
MappedOidcUser( Set<GrantedAuthority> authorities, OidcIdToken idToken, OidcUserInfo userInfo, String nameAttributeKey, String username) { super(authorities, idToken, userInfo, nameAttributeKey); this.username = requireNonNull(username); }