Java Code Examples for org.springframework.security.oauth2.common.OAuth2AccessToken#getExpiresIn()

The following examples show how to use org.springframework.security.oauth2.common.OAuth2AccessToken#getExpiresIn() . 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: RefreshTokenFilter.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Check if we must refresh the access token.
 * We must refresh it, if we either have no access token, or it is expired, or it is about to expire.
 *
 * @param accessTokenCookie the current access token.
 * @return true, if it must be refreshed; false, otherwise.
 */
private boolean mustRefreshToken(Cookie accessTokenCookie) {
    if (accessTokenCookie == null) {
        return true;
    }
    OAuth2AccessToken token = tokenStore.readAccessToken(accessTokenCookie.getValue());
    //check if token is expired or about to expire
    if (token.isExpired() || token.getExpiresIn() < REFRESH_WINDOW_SECS) {
        return true;
    }
    return false;       //access token is still fine
}
 
Example 3
Source File: PigRedisTokenStore.java    From pig with MIT License 5 votes vote down vote up
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {

    this.redisTemplate.opsForValue().set(ACCESS + token.getValue(), token);
    this.redisTemplate.opsForValue().set(AUTH + token.getValue(), authentication);
    this.redisTemplate.opsForValue().set(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), token);
    if (!authentication.isClientOnly()) {
        redisTemplate.opsForList().rightPush(UNAME_TO_ACCESS + getApprovalKey(authentication), token);
    }

    redisTemplate.opsForList().rightPush(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), token);

    if (token.getExpiration() != null) {

        int seconds = token.getExpiresIn();
        redisTemplate.expire(ACCESS + token.getValue(), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(AUTH + token.getValue(), seconds, TimeUnit.SECONDS);

        redisTemplate.expire(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(UNAME_TO_ACCESS + getApprovalKey(authentication), seconds, TimeUnit.SECONDS);
    }
    if (token.getRefreshToken() != null && token.getRefreshToken().getValue() != null) {
        this.redisTemplate.opsForValue().set(REFRESH_TO_ACCESS + token.getRefreshToken().getValue(), token.getValue());
        this.redisTemplate.opsForValue().set(ACCESS_TO_REFRESH + token.getValue(), token.getRefreshToken().getValue());
    }
}
 
Example 4
Source File: FwRedisTokenStore.java    From fw-cloud-framework with MIT License 5 votes vote down vote up
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {

	this.redisTemplate.opsForValue().set(ACCESS + token.getValue(), token);
	this.redisTemplate.opsForValue().set(AUTH + token.getValue(), authentication);
	this.redisTemplate.opsForValue().set(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), token);
	if (!authentication.isClientOnly()) {
		this.redisTemplate.opsForList().rightPush(UNAME_TO_ACCESS + getApprovalKey(authentication), token);
	}

	redisTemplate.opsForList().rightPush(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), token);

	if (token.getExpiration() != null) {

		int seconds = token.getExpiresIn();
		redisTemplate.expire(ACCESS + token.getValue(), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(AUTH + token.getValue(), seconds, TimeUnit.SECONDS);

		redisTemplate.expire(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(UNAME_TO_ACCESS + getApprovalKey(authentication), seconds, TimeUnit.SECONDS);
	}
	if (token.getRefreshToken() != null && token.getRefreshToken()
			.getValue() != null) {
		this.redisTemplate.opsForValue().set(REFRESH_TO_ACCESS + token.getRefreshToken().getValue(), token.getValue());
		this.redisTemplate.opsForValue().set(ACCESS_TO_REFRESH + token.getValue(), token.getRefreshToken().getValue());
	}
}
 
Example 5
Source File: RenewFilter.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
private void doSomething(RequestContext requestContext) {
	HttpServletRequest request = requestContext.getRequest();
	String token = StringUtils.substringAfter(request.getHeader(HttpHeaders.AUTHORIZATION), "bearer ");
	if (StringUtils.isEmpty(token)) {
		return;
	}
	OAuth2AccessToken oAuth2AccessToken = jwtTokenStore.readAccessToken(token);
	int expiresIn = oAuth2AccessToken.getExpiresIn();

	if (expiresIn < EXPIRES_IN) {
		HttpServletResponse servletResponse = requestContext.getResponse();
		servletResponse.addHeader("Renew-Header", "true");
	}
}
 
Example 6
Source File: TokenService.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
/**
 * Chooses a token among all tokens for this user in the token store.
 * 
 * @param userName the username
 * @return the chosen token, or null if no token was found
 */
public OAuth2AccessToken getToken(String userName) {
    OAuth2AccessToken token = null;
    Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByUserName(userName);
    for (OAuth2AccessToken tokenx : tokens) {
        // If a token is already found, overwrite it if the new token:
        // 1) has a refresh token, and the current token hasn't, or
        // 2) expires later than the current token
        if (token == null || ((tokenx.getRefreshToken() != null) && (token.getRefreshToken() == null))
            || (tokenx.getExpiresIn() > token.getExpiresIn())) {
            token = tokenx;
        }
    }
    return token;
}
 
Example 7
Source File: RefreshTokenFilter.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Check if we must refresh the access token.
 * We must refresh it, if we either have no access token, or it is expired, or it is about to expire.
 *
 * @param accessTokenCookie the current access token.
 * @return true, if it must be refreshed; false, otherwise.
 */
private boolean mustRefreshToken(Cookie accessTokenCookie) {
    if (accessTokenCookie == null) {
        return true;
    }
    OAuth2AccessToken token = tokenStore.readAccessToken(accessTokenCookie.getValue());
    //check if token is expired or about to expire
    if (token.isExpired() || token.getExpiresIn() < REFRESH_WINDOW_SECS) {
        return true;
    }
    return false;       //access token is still fine
}