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


Java OAuth2Request.getScope方法代碼示例

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


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

示例1: validateToken

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
@RequestMapping(value = "/validation", method = RequestMethod.POST)
@ResponseBody
public AccessToken validateToken(@RequestHeader("Authorization") final String authorization) {
    String token = getToken(authorization);
    OAuth2Authentication auth = tokenStore.readAuthentication(token);
    OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
    OAuth2Request authReq = auth.getOAuth2Request();

    AccessToken.Builder tokenBuilder = new AccessToken.Builder(token).setClientId(authReq.getClientId());

    if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
        User user = (User) auth.getPrincipal();
        tokenBuilder.setUserName(user.getUserName());
        tokenBuilder.setUserId(user.getId());
    }

    tokenBuilder.setExpiresAt(accessToken.getExpiration());
    for (String scopeString : authReq.getScope()) {
        tokenBuilder.addScope(new Scope(scopeString));
    }

    return tokenBuilder.build();
}
 
開發者ID:osiam,項目名稱:auth-server,代碼行數:24,代碼來源:TokenController.java

示例2: extractKey

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
@Override
public String extractKey(OAuth2Authentication authentication) {
    Map<String, String> values = new LinkedHashMap<>();
    OAuth2Request authorizationRequest = authentication.getOAuth2Request();
    if (!authentication.isClientOnly()) {
        values.put(USERNAME, authentication.getName());
    }
    values.put(CLIENT_ID, authorizationRequest.getClientId());
    if (authorizationRequest.getScope() != null) {
        values.put(SCOPE, OAuth2Utils.formatParameterList(authorizationRequest.getScope()));
    }
    String uuid = UUID.randomUUID().toString();
    values.put(UUID_KEY, uuid);

    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");
    }

    byte[] bytes = digest.digest(values.toString().getBytes(StandardCharsets.UTF_8));
    return String.format("%032x", new BigInteger(1, bytes));
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:25,代碼來源:UniqueAuthenticationKeyGenerator.java

示例3: validateToken

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
public AccessToken validateToken(final String token) {
    OAuth2Authentication auth = tokenStore.readAuthentication(token);
    OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
    OAuth2Request authReq = auth.getOAuth2Request();

    AccessToken.Builder tokenBuilder = new AccessToken.Builder(token).setClientId(authReq.getClientId());

    if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
        User user = (User) auth.getPrincipal();
        tokenBuilder.setUserName(user.getUserName());
        tokenBuilder.setUserId(user.getId());
    }

    tokenBuilder.setExpiresAt(accessToken.getExpiration());
    for (String scopeString : authReq.getScope()) {
        tokenBuilder.addScope(new Scope(scopeString));
    }

    return tokenBuilder.build();
}
 
開發者ID:osiam,項目名稱:osiam,代碼行數:21,代碼來源:TokenService.java

示例4: authenticateUser

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
@BodyParser.Of(BodyParser.Json.class)
@PreAuthorize("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.hasScope('trust')")
public Promise<Result> authenticateUser() {
  JsonNode json = request().body().asJson();
  String username = json.findPath("username").textValue();
  String password = json.findPath("password").textValue();

  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  OAuth2Request clientAuthenticationRequest =
      ((OAuth2Authentication) authentication).getOAuth2Request();
  Map<String, String> requestParameters = new HashMap<>();
  requestParameters.put("username", username);
  requestParameters.put("password", password);
  TokenRequest tokenRequest = new TokenRequest(requestParameters,
          clientAuthenticationRequest.getClientId(), clientAuthenticationRequest.getScope(),
          "password");
  OAuth2AccessToken token = tokenGranter.grant("password", tokenRequest);
  ObjectNode result = Json.newObject();
  result.setAll(ImmutableMap.of(
      "accessToken", result.textNode(token.getValue()),
      "username", result.textNode(username),
      "expiration", result.numberNode(token.getExpiration().getTime()),
      "refreshToken", result.textNode(token.getRefreshToken().getValue())));
  return Promise.pure(ok(result));
}
 
開發者ID:tfeng,項目名稱:play-oauth2,代碼行數:26,代碼來源:SecurityController.java

示例5: refreshUserAccessToken

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
@BodyParser.Of(BodyParser.Json.class)
@PreAuthorize("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.hasScope('trust')")
public Promise<Result> refreshUserAccessToken() {
  JsonNode body = request().body().asJson();
  String refreshToken = body.findPath("refreshToken").textValue();

  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  OAuth2Request clientAuthenticationRequest =
      ((OAuth2Authentication) authentication).getOAuth2Request();
  TokenRequest tokenRequest =
      new TokenRequest(Collections.emptyMap(), clientAuthenticationRequest.getClientId(),
          clientAuthenticationRequest.getScope(), "refresh");
  OAuth2AccessToken token = tokenServices.refreshAccessToken(refreshToken, tokenRequest);
  ObjectNode result = Json.newObject();
  result.setAll(ImmutableMap.of(
      "accessToken", result.textNode(token.getValue()),
      "expiration", result.numberNode(token.getExpiration().getTime()),
      "refreshToken", result.textNode(token.getRefreshToken().getValue())));
  return Promise.pure(ok(result));
}
 
開發者ID:tfeng,項目名稱:play-oauth2,代碼行數:21,代碼來源:SecurityController.java

示例6: createRefreshedAuthentication

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
/**
 * Create a refreshed authentication taking into account the requested scope and the scope of the original
 * authentication.
 * 
 * @param authentication The authentication.
 * @param scope The scope for the refreshed token.
 * @return The refreshed authentication.
 * @throws InvalidScopeException If the scope requested is invalid or wider than the original scope.
 */
private OAuth2Authentication createRefreshedAuthentication(OAuth2Authentication authentication, Set<String> scope) {
	OAuth2Authentication narrowed = authentication;
	if (scope != null && !scope.isEmpty()) {
		OAuth2Request clientAuth = authentication.getOAuth2Request();
		Set<String> originalScope = clientAuth.getScope();
		if (originalScope == null || !originalScope.containsAll(scope)) {
			throw new InvalidScopeException("Unable to narrow the scope of the client authentication to " + scope
					+ ".", originalScope);
		}
		else {
			narrowed = new OAuth2Authentication(clientAuth.narrowScope(scope),
					authentication.getUserAuthentication());
		}
	}
	return narrowed;
}
 
開發者ID:jungyang,項目名稱:oauth-client-master,代碼行數:26,代碼來源:JwtTokenServices.java

示例7: createRefreshedAuthentication

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
/**
 * Create a refreshed authentication.
 * 
 * @param authentication
 *            The authentication.
 * @param scope
 *            The scope for the refreshed token.
 * @return The refreshed authentication.
 * @throws InvalidScopeException
 *             If the scope requested is invalid or wider than the original
 *             scope.
 */
private OAuth2Authentication createRefreshedAuthentication(
		OAuth2Authentication authentication, Set<String> scope) {
	OAuth2Authentication narrowed = authentication;
	if (scope != null && !scope.isEmpty()) {
		OAuth2Request clientAuth = authentication.getOAuth2Request();
		Set<String> originalScope = clientAuth.getScope();
		if (originalScope == null || !originalScope.containsAll(scope)) {
			throw new InvalidScopeException(
					"Unable to narrow the scope of the client authentication to "
							+ scope + ".", originalScope);
		} else {
			narrowed = new OAuth2Authentication(clientAuth.narrowScope(scope),
					authentication.getUserAuthentication());
		}
	}
	return narrowed;
}
 
開發者ID:jungyang,項目名稱:oauth-client-master,代碼行數:30,代碼來源:DefaultTokenServices.java

示例8: hasAnyScope

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
public static boolean hasAnyScope(Authentication authentication, String[] scopes) {

		if (authentication instanceof OAuth2Authentication) {
			OAuth2Request clientAuthentication = ((OAuth2Authentication) authentication).getOAuth2Request();
			Collection<String> assigned = clientAuthentication.getScope();
			if (assigned != null) {
				for (String scope : scopes) {
					if (assigned.contains(scope)) {
						return true;
					}
				}
			}
		}
	
		return false;
	}
 
開發者ID:jungyang,項目名稱:oauth-client-master,代碼行數:17,代碼來源:OAuth2ExpressionUtils.java

示例9: extractAuthentication

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的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

示例10: vote

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {

		int result = ACCESS_ABSTAIN;

		if (!(authentication instanceof OAuth2Authentication)) {
			return result;
		}

		OAuth2Authentication oauth2Authentication = (OAuth2Authentication) authentication;
		OAuth2Request clientAuthentication = oauth2Authentication.getOAuth2Request();
		ClientDetails client = clientDetailsService.loadClientByClientId(clientAuthentication.getClientId());
		Set<String> scopes = clientAuthentication.getScope();
		if (oauth2Authentication.isClientOnly() && clientAuthoritiesAreScopes) {
			scopes = AuthorityUtils.authorityListToSet(clientAuthentication.getAuthorities());
		}

		for (ConfigAttribute attribute : attributes) {
			if (this.supports(attribute)) {

				result = ACCESS_GRANTED;

				for (String scope : scopes) {
					if (!client.getScope().contains(scope)) {
						result = ACCESS_DENIED;
						break;
					}
				}

				if (result == ACCESS_DENIED && throwException) {
					InsufficientScopeException failure = new InsufficientScopeException(
							"Insufficient scope for this resource", client.getScope());
					throw new AccessDeniedException(failure.getMessage(), failure);
				}

				return result;
			}
		}

		return result;
	}
 
開發者ID:jungyang,項目名稱:oauth-client-master,代碼行數:41,代碼來源:ClientScopeVoter.java

示例11: hasAnyScopeMatching

import org.springframework.security.oauth2.provider.OAuth2Request; //導入方法依賴的package包/類
public static boolean hasAnyScopeMatching(Authentication authentication, String[] scopesRegex) {

		if (authentication instanceof OAuth2Authentication) {
			OAuth2Request clientAuthentication = ((OAuth2Authentication) authentication).getOAuth2Request();
			for (String scope : clientAuthentication.getScope()) {
				for (String regex : scopesRegex) {
					if (scope.matches(regex)) {
						return true;
					}
				}
			}
		}

		return false;
	}
 
開發者ID:jungyang,項目名稱:oauth-client-master,代碼行數:16,代碼來源:OAuth2ExpressionUtils.java


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