当前位置: 首页>>代码示例>>Java>>正文


Java OAuth2Authentication类代码示例

本文整理汇总了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;
}
 
开发者ID:xm-online,项目名称:xm-commons,代码行数:17,代码来源:TokenUtils.java

示例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;
}
 
开发者ID:xm-online,项目名称:xm-uaa,代码行数:20,代码来源:DomainTokenServices.java

示例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);
}
 
开发者ID:xm-online,项目名称:xm-uaa,代码行数:13,代码来源:DomainTokenServices.java

示例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 });
}
 
开发者ID:xienjiang,项目名称:session-cloud,代码行数:21,代码来源:CustomTokenStore.java

示例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);
}
 
开发者ID:Microsoft,项目名称:movie-db-java-on-azure,代码行数:19,代码来源:GlobalControllerAdvice.java

示例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;
}
 
开发者ID:xm-online,项目名称:xm-ms-balance,代码行数:18,代码来源:DomainJwtAccessTokenConverter.java

示例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;
}
 
开发者ID:venus-boot,项目名称:saluki,代码行数:20,代码来源:TokenController.java

示例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;
}
 
开发者ID:PacktPublishing,项目名称:OAuth-2.0-Cookbook,代码行数:17,代码来源:AdditionalClaimsTokenEnhancer.java

示例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;
}
 
开发者ID:PatternFM,项目名称:tokamak,代码行数:25,代码来源:JWTTokenConverter.java

示例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);
}
 
开发者ID:helloworldtang,项目名称:sns-todo,代码行数:20,代码来源:SecurityUtil.java

示例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();
}
 
开发者ID:cypherkey,项目名称:multi-tenant-rest-api,代码行数:27,代码来源:RoleChecker.java

示例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);
}
 
开发者ID:DataAgg,项目名称:DAFramework,代码行数:9,代码来源:CustomUserInfoTokenServices.java

示例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);
}
 
开发者ID:sniperqpc,项目名称:Spring-cloud-gather,代码行数:11,代码来源:CustomUserInfoTokenServices.java

示例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;
}
 
开发者ID:xm-online,项目名称:xm-ms-timeline,代码行数:8,代码来源:TenantInterceptor.java


注:本文中的org.springframework.security.oauth2.provider.OAuth2Authentication类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。