本文整理匯總了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();
}
示例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));
}
示例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();
}
示例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));
}
示例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));
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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;
}
示例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;
}