Java tutorial
/** * Copyright(c)2015 IntelCorporation * * LicensedundertheApacheLicense,Version2.0(the"License"); * youmaynotusethisfileexceptincompliancewiththeLicense. * YoumayobtainacopyoftheLicenseat * * http://www.apache.org/licenses/LICENSE-2.0 * * Unlessrequiredbyapplicablelaworagreedtoinwriting,software * distributedundertheLicenseisdistributedonan"ASIS"BASIS, * WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. * SeetheLicenseforthespecificlanguagegoverningpermissionsand * limitationsundertheLicense. */ package org.trustedanalytics.user.current; import org.trustedanalytics.user.invite.config.AccessTokenDetails; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.oauth2.provider.OAuth2Authentication; import java.util.Collection; import java.util.UUID; public final class AuthDetailsFinder implements UserDetailsFinder { private static final String ADMIN_ROLE = "console.admin"; @Override public UserRole getRole(Authentication authentication) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null || authorities.isEmpty()) return UserRole.USER; boolean isAdmin = authorities.stream().map(GrantedAuthority::getAuthority) .filter(ADMIN_ROLE::equalsIgnoreCase).count() > 0; return isAdmin ? UserRole.ADMIN : UserRole.USER; } @Override public UUID findUserId(Authentication authentication) { if (authentication == null) { throw new IllegalArgumentException("Authentication argument must not be null"); } OAuth2Authentication oauth2 = (OAuth2Authentication) authentication; AccessTokenDetails details = (AccessTokenDetails) oauth2.getUserAuthentication().getDetails(); return details.getUserGuid(); } @Override public String findUserName(Authentication authentication) { if (authentication == null) { throw new IllegalArgumentException("Authentication argument must not be null"); } OAuth2Authentication oauth2 = (OAuth2Authentication) authentication; return oauth2.getName(); } }