org.springframework.security.oauth2.client.http.AccessTokenRequiredException Java Examples

The following examples show how to use org.springframework.security.oauth2.client.http.AccessTokenRequiredException. 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: AuthorizationHeaderUtil.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
public Optional<String> getAuthorizationHeaderFromOAuth2Context() {
    OAuth2AccessToken previousAccessToken = oAuth2RestTemplate.getOAuth2ClientContext().getAccessToken();
    if (previousAccessToken == null) {
        return Optional.empty();
    } else {
        OAuth2AccessToken accessToken;
        try {
            // Get the token from OAuth2ClientContext and refresh it if necessary
            accessToken = oAuth2RestTemplate.getAccessToken();
        } catch (UserRedirectRequiredException e) {
            // It's a refresh failure (because previous token wasn't null)
            // If it's an AJAX Request, this sends a 401 error
            throw new AccessTokenRequiredException("Refreshing access token failed",null);
        }

        String tokenType = accessToken.getTokenType();
        if (!StringUtils.hasText(tokenType)) {
            tokenType = OAuth2AccessToken.BEARER_TYPE;
        }
        String authorizationHeaderValue = String.format("%s %s", tokenType, accessToken.getValue());
        return Optional.of(authorizationHeaderValue);
    }
}
 
Example #2
Source File: AuthorizationHeaderUtil.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
public Optional<String> getAuthorizationHeaderFromOAuth2Context() {
    OAuth2AccessToken previousAccessToken = oAuth2RestTemplate.getOAuth2ClientContext().getAccessToken();
    if (previousAccessToken == null) {
        return Optional.empty();
    } else {
        OAuth2AccessToken accessToken;
        try {
            // Get the token from OAuth2ClientContext and refresh it if necessary
            accessToken = oAuth2RestTemplate.getAccessToken();
        } catch (UserRedirectRequiredException e) {
            // It's a refresh failure (because previous token wasn't null)
            // If it's an AJAX Request, this sends a 401 error
            throw new AccessTokenRequiredException("Refreshing access token failed",null);
        }

        String tokenType = accessToken.getTokenType();
        if (!StringUtils.hasText(tokenType)) {
            tokenType = OAuth2AccessToken.BEARER_TYPE;
        }
        String authorizationHeaderValue = String.format("%s %s", tokenType, accessToken.getValue());
        return Optional.of(authorizationHeaderValue);
    }
}
 
Example #3
Source File: AuthorizationHeaderUtil.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
public Optional<String> getAuthorizationHeaderFromOAuth2Context() {
    OAuth2AccessToken previousAccessToken = oAuth2RestTemplate.getOAuth2ClientContext().getAccessToken();
    if (previousAccessToken == null) {
        return Optional.empty();
    } else {
        OAuth2AccessToken accessToken;
        try {
            // Get the token from OAuth2ClientContext and refresh it if necessary
            accessToken = oAuth2RestTemplate.getAccessToken();
        } catch (UserRedirectRequiredException e) {
            // It's a refresh failure (because previous token wasn't null)
            // If it's an AJAX Request, this sends a 401 error
            throw new AccessTokenRequiredException("Refreshing access token failed",null);
        }

        String tokenType = accessToken.getTokenType();
        if (!StringUtils.hasText(tokenType)) {
            tokenType = OAuth2AccessToken.BEARER_TYPE;
        }
        String authorizationHeaderValue = String.format("%s %s", tokenType, accessToken.getValue());
        return Optional.of(authorizationHeaderValue);
    }
}
 
Example #4
Source File: CaseStandardizingOAuth2RequestAuthenticator.java    From shimmer with Apache License 2.0 6 votes vote down vote up
@Override
public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext clientContext,
        ClientHttpRequest request) {

    OAuth2AccessToken accessToken = clientContext.getAccessToken();
    if (accessToken == null) {
        throw new AccessTokenRequiredException(resource);
    }

    String tokenType = accessToken.getTokenType();

    if (!StringUtils.hasText(tokenType) || tokenType.equalsIgnoreCase(OAuth2AccessToken.BEARER_TYPE)) {
        tokenType = OAuth2AccessToken.BEARER_TYPE; // we'll assume basic bearer token type if none is specified.
    }

    request.getHeaders().set("Authorization", String.format("%s %s", tokenType, accessToken.getValue()));
}
 
Example #5
Source File: MyOAuth2RestTemplate.java    From springboot-security-wechat with Apache License 2.0 5 votes vote down vote up
protected OAuth2AccessToken acquireAccessToken(OAuth2ClientContext oauth2Context) throws UserRedirectRequiredException {
    AccessTokenRequest accessTokenRequest = oauth2Context.getAccessTokenRequest();
    if (accessTokenRequest != null) {
        System.out.println("accesstokeRequest == " + accessTokenRequest.getCurrentUri());
    }
    if(accessTokenRequest == null) {
        throw new AccessTokenRequiredException("No OAuth 2 security context has been established. Unable to access resource '" + this.resource.getId() + "'.", this.resource);
    } else {
        String stateKey = accessTokenRequest.getStateKey();
        if(stateKey != null) {
            System.out.println("stateKey == " + stateKey);
            accessTokenRequest.setPreservedState(oauth2Context.removePreservedState(stateKey));
        }

        OAuth2AccessToken existingToken = oauth2Context.getAccessToken();
        if(existingToken != null) {
            accessTokenRequest.setExistingToken(existingToken);
        }

        OAuth2AccessToken accessToken = null;
        accessToken = this.accessTokenProvider.obtainAccessToken(this.resource, accessTokenRequest);
        if(accessToken != null && accessToken.getValue() != null) {
            oauth2Context.setAccessToken(accessToken);
            return accessToken;
        } else {
            throw new IllegalStateException("Access token provider returned a null access token, which is illegal according to the contract.");
        }
    }
}
 
Example #6
Source File: MyOAuth2ClientAuthenticationProcessingFilter.java    From springboot-security-wechat with Apache License 2.0 5 votes vote down vote up
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
    if(failed instanceof AccessTokenRequiredException) {
        throw failed;
    } else {
        super.unsuccessfulAuthentication(request, response, failed);
    }
}
 
Example #7
Source File: OAuth2FeignRequestInterceptor.java    From spring-cloud-security with Apache License 2.0 5 votes vote down vote up
/**
 * Try to acquire the token using a access token provider.
 * @return valid access token
 * @throws UserRedirectRequiredException in case the user needs to be redirected to an
 * approval page or login page
 */
protected OAuth2AccessToken acquireAccessToken()
		throws UserRedirectRequiredException {
	AccessTokenRequest tokenRequest = oAuth2ClientContext.getAccessTokenRequest();
	if (tokenRequest == null) {
		throw new AccessTokenRequiredException(
				"Cannot find valid context on request for resource '"
						+ resource.getId() + "'.",
				resource);
	}
	String stateKey = tokenRequest.getStateKey();
	if (stateKey != null) {
		tokenRequest.setPreservedState(
				oAuth2ClientContext.removePreservedState(stateKey));
	}
	OAuth2AccessToken existingToken = oAuth2ClientContext.getAccessToken();
	if (existingToken != null) {
		oAuth2ClientContext.setAccessToken(existingToken);
	}
	OAuth2AccessToken obtainableAccessToken;
	obtainableAccessToken = accessTokenProvider.obtainAccessToken(resource,
			tokenRequest);
	if (obtainableAccessToken == null || obtainableAccessToken.getValue() == null) {
		throw new IllegalStateException(
				" Access token provider returned a null token, which is illegal according to the contract.");
	}
	oAuth2ClientContext.setAccessToken(obtainableAccessToken);
	return obtainableAccessToken;
}