本文整理汇总了Java中org.springframework.security.oauth2.provider.OAuth2Authentication类的典型用法代码示例。如果您正苦于以下问题:Java OAuth2Authentication类的具体用法?Java OAuth2Authentication怎么用?Java OAuth2Authentication使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OAuth2Authentication类属于org.springframework.security.oauth2.provider包,在下文中一共展示了OAuth2Authentication类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: oauth2ClientContext
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的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: extractCurrentToken
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
public static String extractCurrentToken() {
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
throw new IllegalStateException("Cannot get current authentication object");
}
if (auth.getDetails() == null) {
return null;
}
if (auth.getDetails() instanceof OAuth2AuthenticationDetails) {
return (OAuth2AuthenticationDetails.class.cast(auth.getDetails())).getTokenValue();
}
if (auth.getDetails() instanceof String) {
return String.valueOf(auth.getDetails());
}
return null;
}
示例3: loadAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的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;
}
示例4: createRefreshToken
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
private OAuth2RefreshToken createRefreshToken(OAuth2Authentication authentication) {
if (!isSupportRefreshToken(authentication.getOAuth2Request())) {
return null;
}
int validitySeconds = getRefreshTokenValiditySeconds(authentication);
String value = UUID.randomUUID().toString();
if (validitySeconds > 0) {
return new DefaultExpiringOAuth2RefreshToken(value, new Date(System.currentTimeMillis()
+ (validitySeconds * 1000L)));
}
return new DefaultOAuth2RefreshToken(value);
}
示例5: storeAccessToken
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
String refreshToken = null;
if (token.getRefreshToken() != null) {
refreshToken = token.getRefreshToken().getValue();
}
if (readAccessToken(token.getValue())!=null) {
removeAccessToken(token.getValue());
}
String bid = getAuthorizationDetail(authentication).get("bid");
String pid = getAuthorizationDetail(authentication).get("pid");
jdbcTemplate.update(insertAccessTokenSql, new Object[] { extractTokenKey(token.getValue()),
new SqlLobValue(serializeAccessToken(token)), authenticationKeyGenerator.extractKey(authentication),
authentication.isClientOnly() ? null : authentication.getName(),
authentication.getOAuth2Request().getClientId(),
new SqlLobValue(serializeAuthentication(authentication)), extractTokenKey(refreshToken),bid,pid}, new int[] {
Types.VARCHAR, Types.BLOB, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BLOB, Types.VARCHAR,Types.VARCHAR,Types.VARCHAR });
}
示例6: globalAttributes
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
@ModelAttribute
public void globalAttributes(Model model) {
Boolean isAllowedToUpdateMovieDB = true;
String displayName = "Anonymous";
if (facebookAppId != null && !facebookAppId.isEmpty()) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof OAuth2Authentication) {
displayName = (String) ((LinkedHashMap) ((OAuth2Authentication) auth).getUserAuthentication().getDetails()).get("name");
isAllowedToUpdateMovieDB = auth.isAuthenticated();
} else {
isAllowedToUpdateMovieDB = false;
}
}
UserInfo userInfo = new UserInfo(displayName, isAllowedToUpdateMovieDB);
model.addAttribute("userInfo", userInfo);
}
示例7: extractAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
final OAuth2Authentication authentication = super.extractAuthentication(map);
final Map<String, String> details = new HashMap<>();
details.put(AUTH_TENANT_KEY, (String) map.get(AUTH_TENANT_KEY));
details.put(AUTH_USER_KEY, (String) map.get(AUTH_USER_KEY));
details.put(AUTH_XM_TOKEN_KEY, (String) map.get(AUTH_XM_TOKEN_KEY));
details.put(AUTH_XM_COOKIE_KEY, (String) map.get(AUTH_XM_COOKIE_KEY));
details.put(AUTH_XM_USER_ID_KEY, (String) map.get(AUTH_XM_USER_ID_KEY));
details.put(AUTH_XM_USER_LOGIN_KEY, (String) map.get(AUTH_XM_USER_LOGIN_KEY));
details.put(AUTH_XM_LOCALE_KEY, (String) map.get(AUTH_XM_LOCALE_KEY));
details.put(AUTH_AE_API_TOKEN_KEY, (String) map.get(AUTH_AE_API_TOKEN_KEY));
details.put(AUTH_CHARGE_API_TOKEN_KEY, (String) map.get(AUTH_CHARGE_API_TOKEN_KEY));
authentication.setDetails(details);
return authentication;
}
示例8: enhance
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的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;
}
示例9: 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;
}
示例10: 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;
}
示例11: getUserName
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
public static String getUserName() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof UsernamePasswordAuthenticationToken) {
return authentication.getName();
}
if (authentication instanceof OAuth2Authentication) {
log.info("third part login.authentication:{}, user {},from {}", authentication, authentication.getName(), NetworkUtil.getRemoteIp());
return authentication.getName();
}
if (authentication instanceof AnonymousAuthenticationToken) {
log.warn(" user {} not login,from {}", authentication.getName(), NetworkUtil.getRemoteIp());
return authentication.getName();
}
log.warn("{} isAuthenticated():{},name:{},details:{}", Flag.BizLogFlag.WARN_CHECK, authentication.isAuthenticated(), authentication.getName(), authentication.getDetails());
throw new ApiBizException(GlobalCode.UNKNOWN);
}
示例12: hasValidRole
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
public static boolean hasValidRole(Principal principal, String company, String user) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
LOGGER.info("Super role is {}", SUPERADMIN);
if (company != null) {
LOGGER.info("Required company role is {}", String.format(COMPANYADMIN, company.toUpperCase()));
}
if (user != null) {
LOGGER.info("Required user role is {}", String.format(USER, user.toUpperCase()));
}
for(GrantedAuthority ga : oAuth2Authentication.getAuthorities()) {
LOGGER.info("Checking {}", ga.getAuthority());
if (ga.getAuthority().equalsIgnoreCase(SUPERADMIN)) {
return true;
} else if (company != null && ga.getAuthority().equalsIgnoreCase(String.format(COMPANYADMIN, company.toUpperCase()))) {
return true;
} else if (user != null && ga.getAuthority().equalsIgnoreCase(String.format(USER, user.toUpperCase()))) {
return true;
}
}
throw new ResourceUnauthorizedException();
}
示例13: extractAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
private OAuth2Authentication extractAuthentication(Map<String, Object> map) {
Object principal = getPrincipal(map);
OAuth2Request request = getRequest(map);
List<GrantedAuthority> authorities = authoritiesExtractor.extractAuthorities(map);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
token.setDetails(map);
return new OAuth2Authentication(request, token);
}
示例14: loadAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
throws AuthenticationException, InvalidTokenException {
Map<String, Object> map = getMap(this.userInfoEndpointUrl, accessToken);
if (map.containsKey("error")) {
this.logger.debug("userinfo returned error: " + map.get("error"));
throw new InvalidTokenException(accessToken);
}
return extractAuthentication(map);
}
示例15: getAuthentication
import org.springframework.security.oauth2.provider.OAuth2Authentication; //导入依赖的package包/类
private static OAuth2Authentication getAuthentication() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof OAuth2Authentication) {
return (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
}
return null;
}