本文整理汇总了Java中org.springframework.security.oauth2.provider.OAuth2Authentication.getUserAuthentication方法的典型用法代码示例。如果您正苦于以下问题:Java OAuth2Authentication.getUserAuthentication方法的具体用法?Java OAuth2Authentication.getUserAuthentication怎么用?Java OAuth2Authentication.getUserAuthentication使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.oauth2.provider.OAuth2Authentication
的用法示例。
在下文中一共展示了OAuth2Authentication.getUserAuthentication方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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);
}
示例3: getRoles
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
public Set<String> getRoles() {
OAuth2Authentication oauth = oauth2Authentication();
if (oauth == null) {
return new HashSet<String>();
}
if (oauth.isClientOnly()) {
return new HashSet<String>();
}
Authentication userAuthentication = oauth.getUserAuthentication();
Collection<? extends GrantedAuthority> authorities = userAuthentication.getAuthorities();
return authorities == null ? new HashSet<String>() : authorities.stream().map(authority -> authority.getAuthority()).collect(Collectors.toSet());
}
示例4: getUsername
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
public String getUsername() {
OAuth2Authentication oauth = oauth2Authentication();
if (oauth == null) {
return null;
}
if (oauth.isClientOnly()) {
return null;
}
Authentication userAuthentication = oauth.getUserAuthentication();
return (String) userAuthentication.getPrincipal();
}
示例5: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
/**
* Values placed into the map will be included in the JWT token and the OAuth 2 response itself.
*/
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
Map<String, Object> map = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());
map.put("sub", authentication.getOAuth2Request().getClientId());
map.put("iss", issuer);
map.put("iat", new Date().getTime() / 1000);
Authentication userAuthentication = authentication.getUserAuthentication();
if (userAuthentication == null) {
customAccessToken.setAdditionalInformation(map);
return super.enhance(customAccessToken, authentication);
}
AuthenticatedAccount account = getUser(userAuthentication);
if (account == null) {
customAccessToken.setAdditionalInformation(map);
return super.enhance(customAccessToken, authentication);
}
map.put("sub", account.getIdentfifier());
customAccessToken.setAdditionalInformation(map);
return super.enhance(customAccessToken, authentication);
}
示例6: 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());
}
示例7: user
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
/**
* 获取用户信息,可以定制
*
* @param principal
* @return
*/
@RequestMapping("/user")
public RichUserDetails user(Principal principal) {
if (principal != null) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
Authentication authentication = oAuth2Authentication.getUserAuthentication();
RichUserDetails richUserDetails = (RichUserDetails) authentication.getPrincipal();
return richUserDetails;
}
return null;
}
示例8: getApprovalKey
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
public static String getApprovalKey(OAuth2Authentication authentication) {
String userName = authentication.getUserAuthentication() == null ? "" : authentication.getUserAuthentication().getName();
return getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);
}
示例9: getUserAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
public static Authentication getUserAuthentication() {
OAuth2Authentication auth = getTokenAuthentication();
return auth != null ? auth.getUserAuthentication() : null;
}
示例10: addSocialUser
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
public void addSocialUser(Principal principal) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
Authentication authentication = oAuth2Authentication.getUserAuthentication();
Map<String, String> authenticationDetails = (LinkedHashMap<String, String>) authentication.getDetails();
User user = new User();
user.setName(authenticationDetails.get("name"));
user.setLogin(authenticationDetails.get("id"));
user.setEnabled(true);
Role role = roleRepository.findByName("ROLE_ADMIN");
user.getRoles().add(role);
userRepository.save(user);
}
示例11: socialUser
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入方法依赖的package包/类
@RequestMapping("/socialUser")
public Map<String, Object> socialUser(Principal principal) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
Authentication authentication = oAuth2Authentication.getUserAuthentication();
Map<String, String> authenticationDetails = (LinkedHashMap<String, String>) authentication.getDetails();
UserDetails user = null;
try {
user = userDetailsService.loadUserByLogin(authenticationDetails.get("id"));
} catch (UsernameNotFoundException e) {
}
if (user == null) {
socialLoginAuthorizationManager.addSocialUser(principal);
user = userDetailsService.loadUserByLogin(authenticationDetails.get("id"));
}
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("userAuthentication", authentication.isAuthenticated());
map.put("name", user.getUsername());
map.put("roles", authoritiesAsSet(user.getAuthorities()));
return map;
}