本文整理汇总了Java中org.springframework.security.oauth2.common.OAuth2AccessToken类的典型用法代码示例。如果您正苦于以下问题:Java OAuth2AccessToken类的具体用法?Java OAuth2AccessToken怎么用?Java OAuth2AccessToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OAuth2AccessToken类属于org.springframework.security.oauth2.common包,在下文中一共展示了OAuth2AccessToken类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: refreshAccessToken
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Override
public OAuth2AccessToken refreshAccessToken(String refreshTokenValue, TokenRequest tokenRequest) throws AuthenticationException {
logger.info("refresh token:" + refreshTokenValue);
String jti = tokenRequest.getRequestParameters().get("jti");
try {
if ( jti != null )
if ( blackListService.isBlackListed(jti) ) return null;
OAuth2AccessToken token = super.refreshAccessToken(refreshTokenValue, tokenRequest);
blackListService.addToBlackList(jti);
return token;
} catch (TokenBlackListService.TokenNotFoundException e) {
e.printStackTrace();
return null;
}
}
示例2: general
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
public static Filter general(AuthorizationCodeResourceDetails client, ResourceServerProperties resourceServerProperties, String path, OAuth2ClientContext oauth2ClientContext) {
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path){
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult);
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
log.warn(new Gson().toJson(authResult));
log.warn(new Gson().toJson(accessToken));
}
};
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client, oauth2ClientContext);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), client.getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
return oAuth2ClientAuthenticationFilter;
}
示例3: testAccessRootUrlWithOAuth2AccessToken
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Test
public void testAccessRootUrlWithOAuth2AccessToken() throws Exception {
final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setClientId("myclient");
resourceDetails.setClientSecret("mysecret");
resourceDetails.setGrantType("client_credentials");
resourceDetails
.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");
final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();
final String accessTokenAsString = accessToken.getValue();
localSkipperResource.getMockMvc().perform(get("/api").header("Authorization", "bearer " + accessTokenAsString))
.andDo(print()).andExpect(status().isOk());
}
示例4: readAccessTokenFromSecureStore
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
private OAuth2AccessToken readAccessTokenFromSecureStore(String tokenKey) {
if (tokenKey.length() > TOKEN_KEY_MAX_LENGTH) {
throw new IllegalArgumentException(Messages.TOKEN_KEY_FORMAT_NOT_VALID);
}
SqlParameter storeNameParam = new SqlParameter(Types.VARCHAR);
SqlParameter forXsApplicationUserParam = new SqlParameter(Types.BOOLEAN);
SqlParameter keyParam = new SqlParameter(Types.VARCHAR);
SqlOutParameter valueParam = new SqlOutParameter("VALUE", Types.VARBINARY);
List<SqlParameter> paramList = Arrays.asList(storeNameParam, forXsApplicationUserParam, keyParam, valueParam);
Map<String, Object> result = null;
try {
result = callRetrieve(tokenKey, paramList, PROCEDURE_SECURESTORE_RETRIEVE);
} catch (BadSqlGrammarException e) {
throwIfShouldNotIgnore(e, RETRIEVE_PROCEDURE_NAME);
result = callRetrieve(tokenKey, paramList, PROCEDURE_SECURESTORE_RETRIEVE_LEGACY);
}
byte[] tokenBytes = (byte[]) result.get("VALUE");
if (tokenBytes == null) {
throw new IllegalArgumentException(Messages.TOKEN_NOT_FOUND_IN_SECURE_STORE);
}
byte[] decompressedBytes = CompressUtil.decompress(tokenBytes);
OAuth2AccessToken accessToken = TokenUtil.createToken(new String(decompressedBytes, StandardCharsets.UTF_8));
return accessToken;
}
示例5: loadAuthentication
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Override
public OAuth2Authentication loadAuthentication(String accessTokenValue) throws AuthenticationException,
InvalidTokenException {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
if (accessToken == null) {
throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
} else if (accessToken.isExpired()) {
tokenStore.removeAccessToken(accessToken);
throw new InvalidTokenException("Access token expired: " + accessTokenValue.substring(0,200));
}
OAuth2Authentication result = tokenStore.readAuthentication(accessToken);
if (result == null) {
// in case of race condition
throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
}
return result;
}
示例6: convertAccessToken
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的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;
}
示例7: enhance
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
/**
* Loop over the {@link #setTokenEnhancers(List) delegates} passing the result into the next member of the chain.
*
* @see org.springframework.security.oauth2.provider.token.TokenEnhancer#enhance(org.springframework.security.oauth2.common.OAuth2AccessToken,
* org.springframework.security.oauth2.provider.OAuth2Authentication)
*/
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken tempResult = (DefaultOAuth2AccessToken) accessToken;
final Map<String, Object> additionalInformation = new HashMap<String, Object>();
Map<String, String> details = Maps.newHashMap();
Object userDetails = authentication.getUserAuthentication().getDetails();
if (userDetails != null) {
details = (Map<String, String>) userDetails;
}
//you can do extra functions from authentication details
OAuth2AccessToken result = tempResult;
for (TokenEnhancer enhancer : delegates) {
result = enhancer.enhance(result, authentication);
}
return result;
}
示例8: retrieveToken
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
public OAuth2AccessToken retrieveToken(URI target) {
TargetInfos targetInfos = getTokensFromFile();
if (targetInfos == null) {
return null;
}
HashMap<String, String> targetInfo = targetInfos.get(target);
if (targetInfo == null) {
return null;
}
DefaultOAuth2RefreshToken refreshToken = targetInfos.getRefreshToken(targetInfo);
DefaultOAuth2AccessToken token = targetInfos.getToken(targetInfo);
token.setRefreshToken(refreshToken);
return token;
}
示例9: saveToken
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
public void saveToken(URI target, OAuth2AccessToken token, CloudInfo cloudInfo, CloudSpace space) {
TargetInfos targetInfos = getTokensFromFile();
if (targetInfos == null) {
targetInfos = new TargetInfos();
}
HashMap<String, String> targetInfo = targetInfos.get(target);
if (targetInfo == null) {
targetInfo = new LinkedHashMap<String, String>();
}
targetInfos.putToken(targetInfo, token);
targetInfos.putRefreshToken(targetInfo, token.getRefreshToken());
targetInfos.putVersion(targetInfo, cloudInfo.getVersion());
targetInfos.putSpace(targetInfo, space.getMeta().getGuid().toString());
targetInfos.putOrganization(targetInfo, space.getOrganization().getMeta().getGuid().toString());
targetInfos.put(target, targetInfo);
saveTokensToFile(targetInfos);
}
示例10: enhance
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
private Collection<OAuth2AccessToken> enhance(Collection<OAuth2AccessToken> tokens) {
Collection<OAuth2AccessToken> result = new ArrayList<OAuth2AccessToken>();
for (OAuth2AccessToken prototype : tokens) {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(prototype);
OAuth2Authentication authentication = tokenStore.readAuthentication(token);
if (authentication == null) {
continue;
}
String userName = authentication.getName();
if (StringUtils.isEmpty(userName)) {
userName = "Unknown";
}
Map<String, Object> map = new HashMap<String, Object>(token.getAdditionalInformation());
map.put("user_name", userName);
token.setAdditionalInformation(map);
result.add(token);
}
return result;
}
示例11: refreshTokenOnExpiration
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Test
public void refreshTokenOnExpiration() throws Exception {
URL cloudControllerUrl = new URL(CCNG_API_URL);
CloudCredentials credentials = new CloudCredentials(CCNG_USER_EMAIL, CCNG_USER_PASS);
CloudControllerClientFactory factory = new CloudControllerClientFactory(httpProxyConfiguration, CCNG_API_SSL);
CloudControllerClient client = factory.newCloudController(cloudControllerUrl, credentials, CCNG_USER_ORG, CCNG_USER_SPACE);
client.login();
validateClientAccess(client);
OauthClient oauthClient = factory.getOauthClient();
OAuth2AccessToken token = oauthClient.getToken();
if (token instanceof DefaultOAuth2AccessToken) {
// set the token expiration to "now", forcing the access token to be refreshed
((DefaultOAuth2AccessToken) token).setExpiration(new Date());
validateClientAccess(client);
} else {
fail("Error forcing expiration of access token");
}
}
示例12: findTokensByClientIdAndUserName
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>();
try {
accessTokens = jdbcTemplate.query(selectAccessTokensFromUserNameAndClientIdSql, new SafeAccessTokenRowMapper(),
userName, clientId);
}
catch (EmptyResultDataAccessException e) {
if (log.isInfoEnabled()) {
log.info("Failed to find access token for clientId " + clientId + " and userName " + userName);
}
}
accessTokens = removeNulls(accessTokens);
return accessTokens;
}
示例13: enhance
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
String roles = "";
List<GrantedAuthority> grantedAuthorities = (List<GrantedAuthority>) customUserDetails.getAuthorities();
for (GrantedAuthority grantedAuthority : grantedAuthorities) {
roles = roles.concat(" " + grantedAuthority.getAuthority());
}
roles = roles.trim();
Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("uuid", customUserDetails.getId());
additionalInfo.put("role", roles);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
示例14: attemptAuthentication
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
try {
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
FacebookUser facebookUser = userIdentity.findOrCreateFrom(accessToken);
repository.save(facebookUser);
Authentication authentication = new UsernamePasswordAuthenticationToken(
facebookUser, null, Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
publish(new AuthenticationSuccessEvent(authentication));
return authentication;
} catch (OAuth2Exception e) {
BadCredentialsException error = new BadCredentialsException(
"Cannot retrieve the access token", e);
publish(new OAuth2AuthenticationFailureEvent(error));
throw error;
}
}
示例15: attemptAuthentication
import org.springframework.security.oauth2.common.OAuth2AccessToken; //导入依赖的package包/类
@Override
public Authentication attemptAuthentication(
HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
try {
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
Claims claims = Claims.createFrom(jsonMapper, accessToken);
GoogleUser googleUser = userIdentity.findOrCreateFrom(claims);
repository.save(googleUser);
Authentication authentication = new UsernamePasswordAuthenticationToken(
googleUser, null, googleUser.getAuthorities());
publish(new AuthenticationSuccessEvent(authentication));
return authentication;
} catch (OAuth2Exception e) {
BadCredentialsException error = new BadCredentialsException(
"Cannot retrieve the access token", e);
publish(new OAuth2AuthenticationFailureEvent(error));
throw error;
}
}