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

The following examples show how to use org.springframework.security.oauth2.common.DefaultOAuth2AccessToken#setExpiration() . 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: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
    OAuth2Authentication auth2Authentication = readAuthentication(token.getValue());
    //是否开启token续签
    boolean isRenew = securityProperties.getAuth().getRenew().getEnable();
    if (isRenew && auth2Authentication != null) {
        OAuth2Request clientAuth = auth2Authentication.getOAuth2Request();
        //判断当前应用是否需要自动续签
        if (checkRenewClientId(clientAuth.getClientId())) {
            //获取过期时长
            int validitySeconds = getAccessTokenValiditySeconds(clientAuth.getClientId());
            if (validitySeconds > 0) {
                double expiresRatio = token.getExpiresIn() / (double)validitySeconds;
                //判断是否需要续签,当前剩余时间小于过期时长的50%则续签
                if (expiresRatio <= securityProperties.getAuth().getRenew().getTimeRatio()) {
                    //更新AccessToken过期时间
                    DefaultOAuth2AccessToken oAuth2AccessToken = (DefaultOAuth2AccessToken) token;
                    oAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
                    storeAccessToken(oAuth2AccessToken, auth2Authentication, true);
                }
            }
        }
    }
    return auth2Authentication;
}
 
Example 2
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 3
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 4
Source File: IHealthShim.java    From shimmer with Apache License 2.0 6 votes vote down vote up
@Override
protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() {

    return new ResponseExtractor<OAuth2AccessToken>() {

        @Override
        public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {

            JsonNode node = new ObjectMapper().readTree(response.getBody());
            String token = Preconditions
                    .checkNotNull(node.path("AccessToken").textValue(), "Missing access token: %s", node);
            String refreshToken = Preconditions
                    .checkNotNull(node.path("RefreshToken").textValue(), "Missing refresh token: %s" + node);
            String userId =
                    Preconditions.checkNotNull(node.path("UserID").textValue(), "Missing UserID: %s", node);
            long expiresIn = node.path("Expires").longValue() * 1000;
            Preconditions.checkArgument(expiresIn > 0, "Missing Expires: %s", node);

            DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(token);
            accessToken.setExpiration(new Date(System.currentTimeMillis() + expiresIn));
            accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
            accessToken.setAdditionalInformation(ImmutableMap.<String, Object>of("UserID", userId));
            return accessToken;
        }
    };
}
 
Example 5
Source File: YamiTokenServices.java    From mall4j with GNU Affero General Public License v3.0 5 votes vote down vote up
private OAuth2AccessToken createAccessToken(OAuth2Authentication authentication, OAuth2RefreshToken refreshToken) {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(UUID.randomUUID().toString());
    int validitySeconds = getAccessTokenValiditySeconds(authentication.getOAuth2Request());
    if (validitySeconds > 0) {
        token.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
    }
    token.setRefreshToken(refreshToken);
    token.setScope(authentication.getOAuth2Request().getScope());

    return accessTokenEnhancer != null ? accessTokenEnhancer.enhance(token, authentication) : token;
}
 
Example 6
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 5 votes vote down vote up
public static OAuth2AccessToken createAccessToken(String accessTokenValue, String refreshTokenValue) {
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(accessTokenValue);
    accessToken.setExpiration(new Date());          //token expires now
    DefaultOAuth2RefreshToken refreshToken = new DefaultOAuth2RefreshToken(refreshTokenValue);
    accessToken.setRefreshToken(refreshToken);
    return accessToken;
}
 
Example 7
Source File: OAuth2AccessTokenBuilder.java    From spring-security-mongo with MIT License 5 votes vote down vote up
public OAuth2AccessToken build() {
    final DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(token);
    oAuth2AccessToken.setExpiration(convertToDateFrom(expiration));
    oAuth2AccessToken.setRefreshToken(oAuth2RefreshToken);
    oAuth2AccessToken.setScope(scope);
    oAuth2AccessToken.setAdditionalInformation(additionalInformation);
    return oAuth2AccessToken;
}
 
Example 8
Source File: SmartlingAuthorizationCodeAccessTokenProvider.java    From mojito with Apache License 2.0 5 votes vote down vote up
DefaultOAuth2AccessToken getDefaultOAuth2AccessToken(DateTime now, AuthenticationResponse authenticationResponse) {
    AuthenticationData data = authenticationResponse.getData();
    DefaultOAuth2AccessToken defaultOAuth2AccessToken = new DefaultOAuth2AccessToken(data.getAccessToken());
    defaultOAuth2AccessToken.setExpiration(now.plusSeconds(data.getExpiresIn()).toDate());
    defaultOAuth2AccessToken.setRefreshToken(new DefaultExpiringOAuth2RefreshToken(
            data.getRefreshToken(),
            now.plusSeconds(data.getRefreshExpiresIn()).toDate()));
    return defaultOAuth2AccessToken;
}
 
Example 9
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 10
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 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);

    String accessToken = clientUser.getAccessToken();
    Calendar expirationDate = clientUser.getAccessTokenValidity();

    if (accessToken == null) return null;

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    oAuth2AccessToken.setExpiration(expirationDate.getTime());

    return oAuth2AccessToken;
}
 
Example 12
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);

    String accessToken = clientUser.getAccessToken();
    Calendar expirationDate = clientUser.getAccessTokenValidity();

    if (accessToken == null) return null;

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    oAuth2AccessToken.setExpiration(expirationDate.getTime());

    return oAuth2AccessToken;
}
 
Example 13
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);

    String accessToken = clientUser.getAccessToken();
    Calendar expirationDate = clientUser.getAccessTokenValidity();

    if (accessToken == null) return null;

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    oAuth2AccessToken.setExpiration(expirationDate.getTime());

    return oAuth2AccessToken;
}
 
Example 14
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 15
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public static OAuth2AccessToken createAccessToken(String accessTokenValue, String refreshTokenValue) {
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(accessTokenValue);
    accessToken.setExpiration(new Date());          //token expires now
    DefaultOAuth2RefreshToken refreshToken = new DefaultOAuth2RefreshToken(refreshTokenValue);
    accessToken.setRefreshToken(refreshToken);
    return accessToken;
}
 
Example 16
Source File: OAuth2ClientTokenSevices.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) {
    ClientUser clientUser = getClientUser(authentication);

    String accessToken = clientUser.getAccessToken();
    Calendar expirationDate = clientUser.getAccessTokenValidity();

    if (accessToken == null) return null;


    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    oAuth2AccessToken.setExpiration(expirationDate.getTime());
    oAuth2AccessToken.setRefreshToken(new DefaultOAuth2RefreshToken(clientUser.getRefreshToken()));

    return oAuth2AccessToken;
}
 
Example 17
Source File: OAuth2ClientTokenSevices.java    From OAuth-2.0-Cookbook with MIT License 3 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) {

    String accessToken = settings.getAccessToken();
    Calendar expirationDate = settings.getExpiresIn();

    if (accessToken == null) return null;

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    oAuth2AccessToken.setExpiration(expirationDate.getTime());

    return oAuth2AccessToken;
}