本文整理匯總了Java中org.springframework.security.oauth2.common.DefaultOAuth2AccessToken類的典型用法代碼示例。如果您正苦於以下問題:Java DefaultOAuth2AccessToken類的具體用法?Java DefaultOAuth2AccessToken怎麽用?Java DefaultOAuth2AccessToken使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DefaultOAuth2AccessToken類屬於org.springframework.security.oauth2.common包,在下文中一共展示了DefaultOAuth2AccessToken類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: oauth2ClientContext
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public DefaultOAuth2ClientContext oauth2ClientContext() {
DefaultOAuth2ClientContext context = new DefaultOAuth2ClientContext(
new DefaultAccessTokenRequest());
Authentication principal = SecurityContextHolder.getContext()
.getAuthentication();
if (principal instanceof OAuth2Authentication) {
OAuth2Authentication authentication = (OAuth2Authentication) principal;
Object details = authentication.getDetails();
if (details instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails oauthsDetails = (OAuth2AuthenticationDetails) details;
String token = oauthsDetails.getTokenValue();
context.setAccessToken(new DefaultOAuth2AccessToken(token));
}
}
return context;
}
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:19,代碼來源:OAuth2RestOperationsConfiguration.java
示例2: convert
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
private OAuth2AccessToken convert(io.gravitee.am.repository.oauth2.model.OAuth2AccessToken _oAuth2AccessToken) {
DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(_oAuth2AccessToken.getValue());
oAuth2AccessToken.setAdditionalInformation(_oAuth2AccessToken.getAdditionalInformation());
oAuth2AccessToken.setExpiration(_oAuth2AccessToken.getExpiration());
oAuth2AccessToken.setScope(_oAuth2AccessToken.getScope());
oAuth2AccessToken.setTokenType(_oAuth2AccessToken.getTokenType());
// refresh token
io.gravitee.am.repository.oauth2.model.OAuth2RefreshToken _oAuth2RefreshToken = _oAuth2AccessToken.getRefreshToken();
if (_oAuth2RefreshToken != null) {
DefaultExpiringOAuth2RefreshToken oAuth2RefreshToken =
new DefaultExpiringOAuth2RefreshToken(_oAuth2AccessToken.getRefreshToken().getValue(), _oAuth2AccessToken.getRefreshToken().getExpiration());
oAuth2AccessToken.setRefreshToken(oAuth2RefreshToken);
}
return oAuth2AccessToken;
}
示例3: extractAccessToken
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(value);
Map<String, Object> info = new HashMap<String, Object>(map);
info.remove(EXP);
info.remove(AUD);
info.remove(CLIENT_ID);
info.remove(SCOPE);
if (map.containsKey(EXP)) {
token.setExpiration(new Date((Integer) map.get(EXP) * 1000L));
}
if (map.containsKey(JTI)) {
info.put(JTI, map.get(JTI));
}
@SuppressWarnings("unchecked")
Collection<String> scope = (Collection<String>) map.get(SCOPE);
if (scope != null) {
token.setScope(new HashSet<String>(scope));
}
token.setAdditionalInformation(info);
return token;
}
示例4: setup
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
@Before
public void setup() {
resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(serverRunning.getUrl("/sparklr2/oauth/token"));
resource.setClientId("my-trusted-client");
resource.setId("sparklr");
resource.setScope(Arrays.asList("trust"));
resource.setUsername("marissa");
resource.setPassword("koala");
OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
existingToken = template.getAccessToken();
((DefaultOAuth2AccessToken) existingToken).setExpiration(new Date(0L));
SecurityContextImpl securityContext = new SecurityContextImpl();
securityContext.setAuthentication(new TestingAuthenticationToken("marissa", "koala", "ROLE_USER"));
SecurityContextHolder.setContext(securityContext);
}
示例5: retrieveToken
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的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;
}
示例6: refreshTokenOnExpiration
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的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");
}
}
示例7: enhance
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
@Override
public OAuth2AccessToken enhance(
OAuth2AccessToken accessToken,
OAuth2Authentication authentication) {
Map<String, Object> additional = new HashMap<>();
ResourceOwnerUserDetails user = (ResourceOwnerUserDetails)
authentication.getPrincipal();
additional.put("email", user.getEmail());
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
token.setAdditionalInformation(additional);
return accessToken;
}
示例8: enhance
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的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;
}
示例9: setUpMocks
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
private void setUpMocks() {
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("testTokenValue");
accessToken.setScope(new HashSet<>());
CloudSpace space = new CloudSpace(null, SPACE, new CloudOrganization(null, ORG));
List<CloudSpace> spaces = new ArrayList<>();
if (hasAccess) {
spaces.add(space);
}
userInfo = new UserInfo(USER_ID, USERNAME, accessToken);
List<String> spaceDevelopersList = new ArrayList<>();
if (hasPermissions) {
spaceDevelopersList.add(USER_ID);
}
when(client.getSpaces()).thenReturn(spaces);
when(client.getSpaceDevelopers2(ORG, SPACE)).thenReturn(spaceDevelopersList);
when(clientProvider.getCloudFoundryClient(userInfo.getToken())).thenReturn(client);
}
示例10: enhance
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的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: testGetAccessTokenForDeletedUser
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
@Test
public void testGetAccessTokenForDeletedUser() throws Exception {
//Test approved request
OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", true);
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(storedOAuth2Request, new TestAuthentication("test", true));
OAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
assertEquals(expectedOAuth2AccessToken, getTokenStore().getAccessToken(expectedAuthentication));
assertEquals(expectedAuthentication, getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()));
//Test unapproved request
storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", false);
OAuth2Authentication anotherAuthentication = new OAuth2Authentication(storedOAuth2Request, new TestAuthentication("test", true));
assertEquals(expectedOAuth2AccessToken, getTokenStore().getAccessToken(anotherAuthentication));
// The generated key for the authentication is the same as before, but the two auths are not equal. This could
// happen if there are 2 users in a system with the same username, or (more likely), if a user account was
// deleted and re-created.
assertEquals(anotherAuthentication.getUserAuthentication(), getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()).getUserAuthentication());
// The authorizationRequest does not match because it is unapproved, but the token was granted to an approved request
assertFalse(storedOAuth2Request.equals(getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()).getOAuth2Request()));
}
示例12: accessTokenConverter
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
/**
* Jwt資源令牌轉換器
* @return accessTokenConverter
*/
@Bean
public JwtAccessTokenConverter accessTokenConverter(){
return new JwtAccessTokenConverter(){
/**
* 重寫增強token的方法
* @param accessToken 資源令牌
* @param authentication 認證
* @return 增強的OAuth2AccessToken對象
*/
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
String userName = authentication.getUserAuthentication().getName();
User user = (User) authentication.getUserAuthentication().getPrincipal();
Map<String,Object> infoMap = new HashMap<>();
infoMap.put("userName",userName);
infoMap.put("roles",user.getAuthorities());
((DefaultOAuth2AccessToken)accessToken).setAdditionalInformation(infoMap);
return super.enhance(accessToken, authentication);
}
};
}
示例13: enhance
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的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;
}
示例14: enhanceTokenScopes
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
private OAuth2AccessToken enhanceTokenScopes(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
// enhance token scopes with user permissions
if (clientDetails != null && clientDetails instanceof DelegateClientDetails) {
Client client = ((DelegateClientDetails) clientDetails).getClient();
if (!authentication.isClientOnly()
&& client.isEnhanceScopesWithUserPermissions()
&& authentication.getUserAuthentication().getPrincipal() instanceof User) {
User user = (User) authentication.getUserAuthentication().getPrincipal();
if (user.getRoles() != null && !user.getRoles().isEmpty()) {
Set<Role> roles = roleService.findByIdIn(user.getRoles());
Set<String> enhanceScopes = new HashSet<>(accessToken.getScope());
enhanceScopes.addAll(roles.stream().map(r -> r.getPermissions()).flatMap(List::stream).collect(Collectors.toList()));
((DefaultOAuth2AccessToken) accessToken).setScope(enhanceScopes);
}
}
}
return accessToken;
}
示例15: shouldCreateToken
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; //導入依賴的package包/類
@Test
public void shouldCreateToken() throws Exception {
OAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken("ab66tfz3mw");
when(oAuth2AccessTokenService.getGatewayAccessToken(tenant, application, gateway))
.thenReturn(ServiceResponseBuilder.<OAuth2AccessToken> ok().withResult(oAuth2AccessToken).build());
getMockMvc().perform(MockMvcRequestBuilders
.get(MessageFormat.format("/{0}/{1}/{2}/token", application.getName(), BASEPATH, gateway.getGuid()))
.contentType("application/json")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.code", is(HttpStatus.OK.value())))
.andExpect(jsonPath("$.status", is("success")))
.andExpect(jsonPath("$.timestamp",greaterThan(1400000000)))
.andExpect(jsonPath("$.result").isMap())
.andExpect(jsonPath("$.result.access_token", is("ab66tfz3mw")))
;
}