本文整理匯總了Java中org.springframework.security.oauth2.provider.OAuth2Authentication.getPrincipal方法的典型用法代碼示例。如果您正苦於以下問題:Java OAuth2Authentication.getPrincipal方法的具體用法?Java OAuth2Authentication.getPrincipal怎麽用?Java OAuth2Authentication.getPrincipal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.security.oauth2.provider.OAuth2Authentication
的用法示例。
在下文中一共展示了OAuth2Authentication.getPrincipal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getAccessTokenValiditySeconds
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
/**
* The access token validity period in seconds.
*
* @param authentication the current authentication
* @return the access token validity period in seconds
*/
protected int getAccessTokenValiditySeconds(OAuth2Authentication authentication) {
Integer validity;
Object principal = authentication.getPrincipal();
if (principal instanceof DomainUserDetails) {
validity = DomainUserDetails.class.cast(principal).getAccessTokenValiditySeconds();
if (validity != null) {
return validity;
}
}
validity = tenantPropertiesService.getTenantProps().getSecurity().getAccessTokenValiditySeconds();
if (validity != null) {
return validity;
}
validity = applicationProperties.getSecurity().getAccessTokenValiditySeconds();
if (validity != null) {
return validity;
}
return accessTokenValiditySeconds;
}
示例2: getRefreshTokenValiditySeconds
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
/**
* The refresh token validity period in seconds.
*
* @param authentication the current authentication
* @return the refresh token validity period in seconds
*/
protected int getRefreshTokenValiditySeconds(OAuth2Authentication authentication) {
Integer validity;
Object principal = authentication.getPrincipal();
if (principal instanceof DomainUserDetails) {
validity = DomainUserDetails.class.cast(principal).getRefreshTokenValiditySeconds();
if (validity != null) {
return validity;
}
}
validity = tenantPropertiesService.getTenantProps().getSecurity().getRefreshTokenValiditySeconds();
if (validity != null) {
return validity;
}
validity = applicationProperties.getSecurity().getRefreshTokenValiditySeconds();
if (validity != null) {
return validity;
}
return refreshTokenValiditySeconds;
}
示例3: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的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;
}
示例4: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的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;
}
示例5: setUserTenantContext
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
private String setUserTenantContext(HttpServletRequest request) {
String tenant;
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
tenant = request.getHeader(HEADER_TENANT);
TenantContext.setCurrent(tenant);
} else {
Map<String, String> details = null;
if (auth.getDetails() != null) {
details = Map.class.cast(OAuth2AuthenticationDetails.class.cast(auth.getDetails())
.getDecodedDetails());
}
details = firstNonNull(details, new HashMap<>());
tenant = details.getOrDefault(AUTH_TENANT_KEY, "");
String xmUserKey = details.getOrDefault(AUTH_USER_KEY, "");
String userLogin = (String) auth.getPrincipal();
TenantContext.setCurrent(new TenantInfo(tenant, userLogin, xmUserKey));
}
return tenant;
}
示例6: setCustomDetails
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
private void setCustomDetails(OAuth2Authentication authentication) {
final Map<String, Object> details = new HashMap<>();
final Object principal = authentication.getPrincipal();
details.put(AUTH_TENANT_KEY, TenantContext.getCurrent().getTenant());
if (principal instanceof DomainUserDetails) {
final DomainUserDetails userDetails = (DomainUserDetails) principal;
details.put(AUTH_TENANT_KEY, userDetails.getTenant());
details.put(AUTH_USER_KEY, userDetails.getUserKey());
}
authentication.setDetails(details);
}
示例7: getAuthenticatedUserFromAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
private AuthenticatedUser getAuthenticatedUserFromAuthentication(OAuth2Authentication authentication) {
Object principal = authentication.getPrincipal();
if (principal instanceof AuthenticatedUser) {
return (AuthenticatedUser) principal;
} else {
throw new IllegalStateException("Wrong principal type");
}
}
示例8: endPointUser
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@PreAuthorize("hasRole('ROLE_USER')")
@RequestMapping(value = "/endpointuser", method = RequestMethod.GET)
public ResponseEntity<String> endPointUser(OAuth2Authentication authentication) {
OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
Map<String, Object> additionalInfo = tokenServices.readAccessToken(oAuth2AuthenticationDetails.getTokenValue()).getAdditionalInformation();
return new ResponseEntity<String>("Your UUID: " + additionalInfo.get("uuid").toString()
+ " , your username: " + authentication.getPrincipal() + " and your role USER",
HttpStatus.OK);
}
示例9: endPointAdmin
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/endpointadmin", method = RequestMethod.GET)
public ResponseEntity<String> endPointAdmin(OAuth2Authentication authentication) {
OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
Map<String, Object> additionalInfo = tokenServices.readAccessToken(oAuth2AuthenticationDetails.getTokenValue()).getAdditionalInformation();
return new ResponseEntity<String>("Your UUID: " + additionalInfo.get("uuid").toString()
+ " , your username: " + authentication.getPrincipal() + " and your role ADMIN",
HttpStatus.OK);
}
示例10: createAccessToken
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
OAuth2AccessToken token = super.createAccessToken(authentication);
Account account = (Account) authentication.getPrincipal();
String jti = (String) token.getAdditionalInformation().get("jti");
blackListService.addToEnabledList(
account.getId(),
jti,
token.getExpiration().getTime() );
return token;
}
示例11: setUserTenantContext
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
private String setUserTenantContext(HttpServletRequest request) {
String tenant;
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
tenant = request.getHeader(Constants.HEADER_TENANT);
TenantContext.setCurrent(tenant);
} else {
Map<String, String> details = null;
if (auth.getDetails() != null) {
details = Map.class.cast(OAuth2AuthenticationDetails.class.cast(auth.getDetails())
.getDecodedDetails());
}
details = firstNonNull(details, new HashMap<>());
tenant = details.getOrDefault(AUTH_TENANT_KEY, "");
String xmToken = details.getOrDefault(AUTH_XM_TOKEN_KEY, "");
String xmCookie = details.getOrDefault(AUTH_XM_COOKIE_KEY, "");
String xmUserId = details.getOrDefault(AUTH_XM_USERID_KEY, "");
String xmLocale = details.getOrDefault(AUTH_XM_LOCALE_KEY, "");
String xmUserLogin = (String) auth.getPrincipal();
TenantContext.setCurrent(new TenantInfo(tenant, xmToken, xmCookie, xmUserId, xmLocale, xmUserLogin));
Locale locale = LocaleUtils.getLocaleFromString(xmLocale);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}
}
return tenant;
}
示例12: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
final Map<String, Object> additionalInfo = new HashMap<>();
SecurityUser user = (SecurityUser)authentication.getPrincipal();
additionalInfo.put("username", user.getUsername());
additionalInfo.put("authorities", user.getAuthorities());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
示例13: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
User user = (User) oAuth2Authentication.getPrincipal();
final Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("id", user.getId());
additionalInfo.put("username", user.getUsername());
additionalInfo.put("avatar", user.getAvatar());
additionalInfo.put("authorities", user.getAuthorities());
((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(additionalInfo);
return oAuth2AccessToken;
}
示例14: preHandle
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
@SuppressWarnings("unchecked")
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (isIgnoredRequest(request)) {
return true;
}
try {
String tenant;
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
tenant = request.getHeader(HEADER_TENANT);
TenantContext.setCurrent(tenant);
} else {
Map<String, String> details = null;
if (auth.getDetails() != null) {
details = Map.class.cast(OAuth2AuthenticationDetails.class.cast(auth.getDetails())
.getDecodedDetails());
}
if (details == null) {
details = new HashMap<>();
}
tenant = details.getOrDefault(AUTH_TENANT_KEY, "");
String xmToken = details.getOrDefault(AUTH_XM_TOKEN_KEY, "");
String xmCookie = details.getOrDefault(AUTH_XM_COOKIE_KEY, "");
String xmUserId = details.getOrDefault(AUTH_XM_USER_ID_KEY, "");
String xmLocale = details.getOrDefault(AUTH_XM_LOCALE_KEY, "");
String xmUserLogin = (String) auth.getPrincipal();
String xmUserKey = details.getOrDefault(AUTH_USER_KEY, "");
TenantContext.setCurrent(new TenantInfo(tenant, xmToken, xmCookie, xmUserId, xmLocale,
xmUserLogin, xmUserKey));
Locale locale = LocaleUtils.getLocaleFromString(xmLocale);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}
}
final boolean tenantSet;
if (StringUtils.isBlank(tenant)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write("{\"error\": \"No tenant supplied\"}");
response.getWriter().flush();
tenantSet = false;
} else if (tenantListRepository.getSuspendedTenants().contains(tenant.toLowerCase())) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write("{\"error\": \"Tenant is suspended\"}");
response.getWriter().flush();
tenantSet = false;
} else {
tenantSet = true;
}
return tenantSet;
} catch (Exception e) {
log.error("Error in tenant interceptor: ", e);
throw e;
}
}
示例15: preHandle
import org.springframework.security.oauth2.provider.OAuth2Authentication; //導入方法依賴的package包/類
@Override
@SuppressWarnings("unchecked")
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (isIgnoredRequest(request)) {
return true;
}
try {
String tenant;
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
tenant = request.getHeader(HEADER_TENANT);
TenantContext.setCurrent(tenant);
} else {
Map<String, String> details = null;
if (auth.getDetails() != null) {
details = Map.class.cast(OAuth2AuthenticationDetails.class.cast(auth.getDetails())
.getDecodedDetails());
}
if (details == null) {
details = new HashMap<>();
}
tenant = details.getOrDefault(AUTH_TENANT_KEY, "");
String xmToken = details.getOrDefault(AUTH_XM_TOKEN_KEY, "");
String xmCookie = details.getOrDefault(AUTH_XM_COOKIE_KEY, "");
String xmUserId = details.getOrDefault(AUTH_XM_USERID_KEY, "");
String xmLocale = details.getOrDefault(AUTH_XM_LOCALE, "");
String xmUserLogin = (String) auth.getPrincipal();
String xmUserKey = details.getOrDefault(AUTH_USER_KEY, "");
TenantContext.setCurrent(new TenantInfo(tenant, xmToken, xmCookie, xmUserId, xmLocale,
xmUserLogin, xmUserKey));
Locale locale = LocaleUtils.getLocaleFromString(xmLocale);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}
}
final boolean tenantSet;
if (StringUtils.isBlank(tenant)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write("{\"error\": \"No tenant supplied\"}");
response.getWriter().flush();
tenantSet = false;
} else if (tenantListRepository.getSuspendedTenants().contains(tenant.toLowerCase())) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write("{\"error\": \"Tenant is suspended\"}");
response.getWriter().flush();
tenantSet = false;
} else {
tenantSet = true;
}
return tenantSet;
} catch (Exception e) {
log.error("Error in tenant interceptor: ", e);
throw e;
}
}