Java Code Examples for org.springframework.security.oauth2.common.DefaultOAuth2AccessToken#setAdditionalInformation()

The following examples show how to use org.springframework.security.oauth2.common.DefaultOAuth2AccessToken#setAdditionalInformation() . 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: AuthorizationServerConfig.java    From cloud-service with MIT License 6 votes vote down vote up
/**
 * 将当前用户信息追加到登陆后返回的json数据里<br>
 * 通过参数access_token.add-userinfo控制<br>
 * 2019.07.13
 *
 * @param accessToken
 * @param authentication
 */
private void addLoginUserInfo(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    if (!addUserInfo) {
        return;
    }

    if (accessToken instanceof DefaultOAuth2AccessToken) {
        DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken) accessToken;

        Authentication userAuthentication = authentication.getUserAuthentication();
        Object principal = userAuthentication.getPrincipal();
        if (principal instanceof LoginAppUser) {
            LoginAppUser loginUser = (LoginAppUser) principal;

            Map<String, Object> map = new HashMap<>(defaultOAuth2AccessToken.getAdditionalInformation()); // 旧的附加参数
            map.put("loginUser", loginUser); // 追加当前登陆用户

            defaultOAuth2AccessToken.setAdditionalInformation(map);
        }
    }
}
 
Example 2
Source File: AdditionalClaimsTokenEnhancer.java    From OAuth-2.0-Cookbook with MIT License 6 votes vote down vote up
@Override
public OAuth2AccessToken enhance(
    OAuth2AccessToken accessToken,
    OAuth2Authentication authentication) {

    Map<String, Object> additional = new HashMap<>();

    ResourceOwnerUserDetails user = (ResourceOwnerUserDetails)
        authentication.getPrincipal();
    additional.put("email", user.getEmail());

    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
    token.setAdditionalInformation(additional);

    return accessToken;
}
 
Example 3
Source File: OauthAdminController.java    From OpenESPI-DataCustodian-java with Apache License 2.0 6 votes vote down vote up
private Collection<OAuth2AccessToken> enhance(Collection<OAuth2AccessToken> tokens) {
	Collection<OAuth2AccessToken> result = new ArrayList<OAuth2AccessToken>();
	for (OAuth2AccessToken prototype : tokens) {
		DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(prototype);
		OAuth2Authentication authentication = tokenStore.readAuthentication(token);
		if (authentication == null) {
			continue;
		}
		String clientId = authentication.getOAuth2Request().getClientId();
		if (clientId != null) {
			Map<String, Object> map = new HashMap<String, Object>(token.getAdditionalInformation());
			map.put("client_id", clientId);
			token.setAdditionalInformation(map);
			result.add(token);
		}
	}
	return result;
}
 
Example 4
Source File: CustomAccessTokenConverter.java    From spring-boot-2-oauth2-resource-jwt with MIT License 6 votes vote down vote up
public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
	DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(value);
	Map<String, Object> info = new HashMap<String, Object>(map);

	info.remove(EXP);
	info.remove(AUD);
	info.remove(CLIENT_ID);
	info.remove(SCOPE);

	if (map.containsKey(EXP))
		token.setExpiration(new Date((Long) map.get(EXP) * 1000L));

	if (map.containsKey(JTI))
		info.put(JTI, map.get(JTI));

	token.setScope(extractScope(map));
	token.setAdditionalInformation(info);
	return token;
}
 
Example 5
Source File: CustomAccessTokenConverter.java    From microservices-oauth with Apache License 2.0 6 votes vote down vote up
public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
	DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(value);
	Map<String, Object> info = new HashMap<String, Object>(map);

	info.remove(EXP);
	info.remove(AUD);
	info.remove(CLIENT_ID);
	info.remove(SCOPE);

	if (map.containsKey(EXP))
		token.setExpiration(new Date((Long) map.get(EXP) * 1000L));

	if (map.containsKey(JTI))
		info.put(JTI, map.get(JTI));

	token.setScope(extractScope(map));
	token.setAdditionalInformation(info);
	return token;
}
 
Example 6
Source File: CustomJwtTokenEnhancer.java    From fast-family-master with Apache License 2.0 6 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
    if (oAuth2AccessToken instanceof DefaultOAuth2AccessToken) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) oAuth2AccessToken;
        String clientId = oAuth2Authentication.getOAuth2Request().getClientId();
        Date expiration = oAuth2AccessToken.getExpiration();
        String createToken = createToken(clientId, expiration);
        token.setValue(createToken);
        OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken();
        if (refreshToken instanceof DefaultOAuth2AccessToken) {
            token.setRefreshToken(new DefaultOAuth2RefreshToken(createToken(clientId, expiration)));
        }
        Map<String, Object> additionalInformation = new HashMap<>();
        additionalInformation.put("client_id", oAuth2Authentication.getOAuth2Request().getClientId());
        token.setAdditionalInformation(additionalInformation);
        return token;
    }
    return oAuth2AccessToken;
}
 
Example 7
Source File: OsiamTokenEnhancer.java    From osiam with MIT License 6 votes vote down vote up
@Override
public OAuth2AccessToken enhance(final OAuth2AccessToken accessToken, final OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
    Map<String, Object> additionalInformation = new HashMap<>();
    additionalInformation.put("expires_at", token.getExpiration());

    if (token.getRefreshToken() != null) {
        DefaultExpiringOAuth2RefreshToken refreshToken =
                (DefaultExpiringOAuth2RefreshToken) token.getRefreshToken();
        additionalInformation.put("refresh_token_expires_at", refreshToken.getExpiration());
    }

    additionalInformation.put("client_id", authentication.getOAuth2Request().getClientId());

    if (authentication.getUserAuthentication() != null && authentication.getPrincipal() instanceof User) {
        User user = (User) authentication.getPrincipal();
        additionalInformation.put("user_name", user.getUserName());
        additionalInformation.put("user_id", user.getId());
    }

    token.setAdditionalInformation(additionalInformation);

    return accessToken;
}
 
Example 8
Source File: OpenTokenEnhancer.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 生成token
 *
 * @param accessToken
 * @param authentication
 * @return
 */
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken defaultOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    final Map<String, Object> additionalInfo = new HashMap<>(8);
    if (!authentication.isClientOnly()) {
        if (authentication.getPrincipal() != null && authentication.getPrincipal() instanceof OpenUserDetails) {
            // 设置额外用户信息
            OpenUserDetails baseUser = ((OpenUserDetails) authentication.getPrincipal());
            additionalInfo.put(OpenSecurityConstants.OPEN_ID, baseUser.getUserId());
            additionalInfo.put(OpenSecurityConstants.DOMAIN, baseUser.getDomain());
        }
    }
    defaultOAuth2AccessToken.setAdditionalInformation(additionalInfo);
    return super.enhance(defaultOAuth2AccessToken, authentication);
}
 
Example 9
Source File: CustomTokenEnhancer.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    Map<String, Object> additionalInfo = Maps.newHashMap();
    //自定义token内容,加入组织机构信息
    additionalInfo.put("organization", authentication.getName());
 DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken) accessToken;
 defaultOAuth2AccessToken.setAdditionalInformation(additionalInfo);
 return accessToken;
}
 
Example 10
Source File: CustomTokenEnhancer.java    From microservice-integration with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
                                 OAuth2Authentication authentication) {
    CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
    authentication.getUserAuthentication().getPrincipal();
    Map<String, Object> info = new HashMap<>();
    info.put(TOKEN_SEG_USER_ID, userDetails.getUserId());

    DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
    customAccessToken.setAdditionalInformation(info);

    OAuth2AccessToken enhancedToken = super.enhance(customAccessToken, authentication);
    enhancedToken.getAdditionalInformation().put(TOKEN_SEG_CLIENT, userDetails.getClientId());
    return enhancedToken;
}
 
Example 11
Source File: OAuth2ClientTokenSevices.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) {
    ClientUser clientUser = getClientUser(authentication);

    if (clientUser.accessToken == null) return null;

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(clientUser.accessToken);
    oAuth2AccessToken.setAdditionalInformation(clientUser.additionalInformation);
    oAuth2AccessToken.setExpiration(new Date(clientUser.expirationTime));

    return oAuth2AccessToken;
}
 
Example 12
Source File: CustomTokenEnhancer.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    Map<String, Object> additionalInfo = Maps.newHashMap();
    //自定义token内容,加入组织机构信息
    additionalInfo.put("organization", authentication.getName());
 DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken) accessToken;
 defaultOAuth2AccessToken.setAdditionalInformation(additionalInfo);
 return accessToken;
}
 
Example 13
Source File: JweTokenEnhancer.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    Map<String, Object> info = new LinkedHashMap<>(accessToken.getAdditionalInformation());
    String tokenId = result.getValue();
    if (!info.containsKey(TOKEN_ID)) {
        info.put(TOKEN_ID, tokenId);
    }
    result.setAdditionalInformation(info);
    result.setValue(encode(result, authentication));

    return result;
}
 
Example 14
Source File: JweTokenEnhancer.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    Map<String, Object> info = new LinkedHashMap<>(accessToken.getAdditionalInformation());
    String tokenId = result.getValue();
    if (!info.containsKey(TOKEN_ID)) {
        info.put(TOKEN_ID, tokenId);
    }
    result.setAdditionalInformation(info);
    result.setValue(encode(result, authentication));

    return result;
}
 
Example 15
Source File: SecurityUtilTest.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Test
void testGetTokenUserInfo() {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(TOKEN);
    Map<String, Object> additionalInformation = asMap(USER_ID, USER_NAME);
    token.setAdditionalInformation(additionalInformation);
    UserInfo userInfo = SecurityUtil.getTokenUserInfo(token);
    assertEquals(USER_ID, userInfo.getId());
    assertEquals(USER_NAME, userInfo.getName());
    assertEquals(TOKEN, userInfo.getToken()
                                .getValue());
}
 
Example 16
Source File: CustomTokenServicesTest.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private OAuth2AccessToken buildToken(String tokenString, Date expiration, Map<String, Object> additionalTokenProperties) {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(tokenString);
    token.setExpiration(expiration);
    token.setScope(Collections.singleton(DEFAULT_SCOPE));
    token.setAdditionalInformation(additionalTokenProperties);
    return token;
}
 
Example 17
Source File: TokenFactory.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public OAuth2AccessToken createToken(String tokenString, Map<String, Object> tokenInfo) {
    List<String> scope = (List<String>) tokenInfo.get(SCOPE);
    Number exp = (Number) tokenInfo.get(EXP);
    if (scope == null || exp == null) {
        return null;
    }
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(tokenString);
    token.setExpiration(new Date(exp.longValue() * 1000));
    token.setScope(new HashSet<>(scope));
    token.setAdditionalInformation(tokenInfo);
    return token;
}
 
Example 18
Source File: CustomTokenEnhancer.java    From Auth-service with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
                                 OAuth2Authentication authentication) {
    CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
    authentication.getUserAuthentication().getPrincipal();
    Map<String, Object> info = new HashMap<>();
    info.put(TOKEN_SEG_USER_ID, userDetails.getUserId());

    DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
    customAccessToken.setAdditionalInformation(info);

    OAuth2AccessToken enhancedToken = super.enhance(customAccessToken, authentication);
    enhancedToken.getAdditionalInformation().put(TOKEN_SEG_CLIENT, userDetails.getClientId());
    return enhancedToken;
}
 
Example 19
Source File: OAuth2Configuration.java    From spring-boot-2-oauth2-authorization-jwt with MIT License 3 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
	User user = (User) authentication.getPrincipal();

	Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());

	info.put("email", user.getEmail());

	DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
	customAccessToken.setAdditionalInformation(info);

	return super.enhance(customAccessToken, authentication);
}
 
Example 20
Source File: OAuth2Configuration.java    From microservices-oauth with Apache License 2.0 3 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
	User user = (User) authentication.getPrincipal();

	Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());

	info.put("email", user.getEmail());

	DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
	customAccessToken.setAdditionalInformation(info);

	return super.enhance(customAccessToken, authentication);
}