當前位置: 首頁>>代碼示例>>Java>>正文


Java OAuth2Authentication.getOAuth2Request方法代碼示例

本文整理匯總了Java中org.springframework.security.oauth2.provider.OAuth2Authentication.getOAuth2Request方法的典型用法代碼示例。如果您正苦於以下問題:Java OAuth2Authentication.getOAuth2Request方法的具體用法?Java OAuth2Authentication.getOAuth2Request怎麽用?Java OAuth2Authentication.getOAuth2Request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.security.oauth2.provider.OAuth2Authentication的用法示例。


在下文中一共展示了OAuth2Authentication.getOAuth2Request方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: convertAccessToken

import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
/**
 * Values placed into the map will be included in the JWT token only, not the OAuth 2 response itself.
 */
@Override
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
	Map<String, Object> map = (Map<String, Object>) super.convertAccessToken(token, authentication);

	OAuth2Request request = authentication.getOAuth2Request();
	Set<String> authorities = request.getAuthorities().stream().map(a -> a.getAuthority()).collect(Collectors.toSet());

	ClientDetails client = clientAuthenticationService.loadClientByClientId(request.getClientId());
	if (client.getResourceIds() != null && !client.getResourceIds().isEmpty()) {
		map.put(AUDIENCE, client.getResourceIds());
	}

	Authentication userAuthentication = authentication.getUserAuthentication();
	if (userAuthentication == null) {
		map.remove("authorities");
	}

	map.put(CLIENT_AUTHORITIES, authorities);

	return map;
}
 
開發者ID:PatternFM,項目名稱:tokamak,代碼行數:25,代碼來源:JWTTokenConverter.java

示例2: getOAuth2Authentication

import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {

    Map<String, String> parameters = tokenRequest.getRequestParameters();
    String authorizationCode = parameters.get("code");
    String redirectUri = parameters.get(OAuth2Utils.REDIRECT_URI);
    String codeVerifier = parameters.get("code_verifier");

    if (authorizationCode == null) {
        throw new InvalidRequestException("An authorization code must be supplied.");
    }

    OAuth2Authentication storedAuth = authorizationCodeServices.consumeAuthorizationCode(authorizationCode);
    if (storedAuth == null) {
        throw new InvalidGrantException("Invalid authorization code: " + authorizationCode);
    }

    OAuth2Request pendingOAuth2Request = storedAuth.getOAuth2Request();




    // Validates code verifier
    Map<String, String> pendingOauth2RequestParams = pendingOAuth2Request.getRequestParameters();
    String codeChallenge = pendingOauth2RequestParams.get("code_challenge");
    String codeChallengeMethod = pendingOauth2RequestParams.get("code_challenge_method");

    if (codeVerifier == null && codeChallenge != null) {
        // client is using PKCE but did not send the codeVerifier
        throw new InvalidRequestException(
                "Invalid authorization code for current token request.");
    }

    if (codeVerifier != null && codeChallenge != null) {
        String hashed = codeVerifier;
        if ("S256".equals(codeChallengeMethod)) {
            hashed = DigestUtils.sha256Hex(codeVerifier);
        }

        if (!hashed.equalsIgnoreCase(codeChallenge)) {
            throw new InvalidRequestException(
                    "Invalid authorization code for current token request.");
        }
    }



    // https://jira.springsource.org/browse/SECOAUTH-333
    // This might be null, if the authorization was done without the redirect_uri parameter
    String redirectUriApprovalParameter = pendingOAuth2Request.getRequestParameters().get(
            OAuth2Utils.REDIRECT_URI);

    if ((redirectUri != null || redirectUriApprovalParameter != null)
            && !pendingOAuth2Request.getRedirectUri().equals(redirectUri)) {
        throw new RedirectMismatchException("Redirect URI mismatch.");
    }

    String pendingClientId = pendingOAuth2Request.getClientId();
    String clientId = tokenRequest.getClientId();
    if (clientId != null && !clientId.equals(pendingClientId)) {
        // just a sanity check.
        throw new InvalidClientException("Client ID mismatch");
    }

    // Secret is not required in the authorization request, so it won't be available
    // in the pendingAuthorizationRequest. We do want to check that a secret is provided
    // in the token request, but that happens elsewhere.

    Map<String, String> combinedParameters = new HashMap<String, String>(pendingOAuth2Request
            .getRequestParameters());
    // Combine the parameters adding the new ones last so they override if there are any clashes
    combinedParameters.putAll(parameters);

    // Make a new stored request with the combined parameters
    OAuth2Request finalStoredOAuth2Request = pendingOAuth2Request.createOAuth2Request(combinedParameters);

    Authentication userAuth = storedAuth.getUserAuthentication();

    return new OAuth2Authentication(finalStoredOAuth2Request, userAuth);

}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:82,代碼來源:CustomAuthCodeTokenGranter.java

示例3: extractAuthentication

import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
	List<String> authorities = (List<String>) map.get(CLIENT_AUTHORITIES);
	Collection<GrantedAuthority> grantedAuthorities = authorities.stream().map(a -> new SimpleGrantedAuthority(a)).collect(Collectors.toList());

	OAuth2Authentication authentication = super.extractAuthentication(map);
	OAuth2Request request = authentication.getOAuth2Request();
	OAuth2Request enhancedRequest = new OAuth2Request(request.getRequestParameters(), request.getClientId(), grantedAuthorities, request.isApproved(), request.getScope(), request.getResourceIds(), request.getRedirectUri(), request.getResponseTypes(), request.getExtensions());

	return new OAuth2Authentication(enhancedRequest, authentication.getUserAuthentication());
}
 
開發者ID:PatternFM,項目名稱:tokamak,代碼行數:12,代碼來源:JWTTokenConverter.java


注:本文中的org.springframework.security.oauth2.provider.OAuth2Authentication.getOAuth2Request方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。