Java Code Examples for org.springframework.security.oauth2.provider.OAuth2Authentication#getDetails()

The following examples show how to use org.springframework.security.oauth2.provider.OAuth2Authentication#getDetails() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuth2RestOperationsConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: OAuth2Util.java    From DAFramework with MIT License 6 votes vote down vote up
public static EAccessToken fetch(OAuth2Authentication oAuth2Authentication, OAuth2AccessToken accessToken){
	EAccessToken eAccessToken = new EAccessToken();
	eAccessToken.setOpenUser(fetch(oAuth2Authentication));

	Object details = oAuth2Authentication.getDetails();
	if(details instanceof OAuth2AuthenticationDetails){
		OAuth2AuthenticationDetails details1 = (OAuth2AuthenticationDetails) details;
		eAccessToken.setRemoteAddress(details1.getRemoteAddress());
		eAccessToken.setSessionId(details1.getSessionId());
	}
	eAccessToken.setTokenType(accessToken.getTokenType());
	eAccessToken.setTokenValue(accessToken.getValue());
	eAccessToken.setExpiresIn(accessToken.getExpiresIn());
	if (accessToken.getRefreshToken() != null) {
		eAccessToken.setRefreshToken(accessToken.getRefreshToken().getValue());
	}
	if (accessToken.getScope() != null) {
		String scopes = Strings.join2("|", accessToken.getScope().toArray(new String[]{}));
		eAccessToken.setScopes(scopes);
	}
	return eAccessToken;
}
 
Example 3
Source File: HomeController.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@GetMapping("/")
public String home(ModelMap modelMap, Authentication authentication) {
    OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
    modelMap.put("username", oauth2Authentication.getName());
    modelMap.put("authorities", oauth2Authentication.getAuthorities());
    modelMap.put("clientId", oauth2Authentication.getOAuth2Request().getClientId());
    OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)oauth2Authentication.getDetails();
    modelMap.put("token", details.getTokenValue());
    return "index";
}
 
Example 4
Source File: SsoLogoutSuccessHandler.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
	OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
	OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)oauth2Authentication.getDetails();
	String accessToken = details.getTokenValue();
	redirectStrategy.sendRedirect(request, response, logoutUri+accessToken);
}
 
Example 5
Source File: OAuth2BearerPrincipalHeadersCallback.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 5 votes vote down vote up
@Override
protected void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
    OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) userAgentSession.getPrincipal();
    OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) oAuth2Authentication.getDetails();
    String accessToken = details.getTokenValue();
    headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + accessToken));
    if (logger.isDebugEnabled()) {
        logger.debug("Added Oauth2 bearer token authentication header for user " +
                oAuth2Authentication.getName() + " to web sockets http headers");
    }
}
 
Example 6
Source File: SecurityUtils.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
public static OAuth2AuthenticationDetails getTokenDetails() {
    OAuth2Authentication auth = getTokenAuthentication();
    if (auth != null && auth.getDetails() instanceof OAuth2AuthenticationDetails) {
        return (OAuth2AuthenticationDetails) auth.getDetails();
    }
    return null;
}
 
Example 7
Source File: AuthenticationClaimsIntegrationTest.java    From spring-security-oauth with MIT License 5 votes vote down vote up
@Test
public void whenTokenDontContainIssuer_thenSuccess() {
    final String tokenValue = obtainAccessToken("fooClientIdPassword", "john", "123");
    final OAuth2Authentication auth = tokenStore.readAuthentication(tokenValue);
    System.out.println(tokenValue);
    System.out.println(auth);
    assertTrue(auth.isAuthenticated());
    System.out.println(auth.getDetails());

    Map<String, Object> details = (Map<String, Object>) auth.getDetails();
    assertTrue(details.containsKey("organization"));
    System.out.println(details.get("organization"));
}
 
Example 8
Source File: UserController.java    From spring-security-oauth with MIT License 5 votes vote down vote up
@PreAuthorize("#oauth2.hasScope('read')")
@RequestMapping(method = RequestMethod.GET, value = "/users/extra")
@ResponseBody
public Map<String, Object> getExtraInfo(OAuth2Authentication auth) {
    final OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) auth.getDetails();
    final OAuth2AccessToken accessToken = tokenStore.readAccessToken(details.getTokenValue());
    System.out.println(accessToken);
    return accessToken.getAdditionalInformation();
}
 
Example 9
Source File: SysUserServiceImpl.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
@Transactional
@Override
public SysUser updateSysUser(SysUser sysUser) {
	sysUser.setUpdateTime(new Date());

	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

	if (authentication instanceof OAuth2Authentication) {
		OAuth2Authentication oAuth2Auth = (OAuth2Authentication) authentication;
		authentication = oAuth2Auth.getUserAuthentication();

		OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) oAuth2Auth.getDetails();

		LoginAppUser user = SysUserUtil.getLoginAppUser();

		if (user != null) {

			if ( !ObjectUtils.notEqual(user.getId(),sysUser.getId()) ) {

				OAuth2AccessToken token = redisTokenStore.readAccessToken(details.getTokenValue());

				if (token != null) {

					if (!StringUtils.isBlank(sysUser.getHeadImgUrl())) {
						user.setHeadImgUrl(sysUser.getHeadImgUrl());
					}

					if (!StringUtils.isBlank(sysUser.getNewPassword())) {
						user.setPassword(sysUser.getNewPassword());
					}

					if (!StringUtils.isBlank(sysUser.getNewPassword())) {
						user.setPassword(sysUser.getNewPassword());
					}

					if (!StringUtils.isBlank(sysUser.getNickname())) {
						user.setNickname(sysUser.getNickname());
					}

					if (!StringUtils.isBlank(sysUser.getPhone())){
						user.setPhone(sysUser.getPhone());
					}

					if (sysUser.getSex() != null) {
						user.setSex(sysUser.getSex());
					}

					UsernamePasswordAuthenticationToken userAuthentication = new UsernamePasswordAuthenticationToken(user,
	                        null, user.getAuthorities());

					OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Auth.getOAuth2Request(), userAuthentication);
					oAuth2Authentication.setAuthenticated(true);
					redisTokenStore.storeAccessToken(token, oAuth2Authentication);

				}

			}

		}
	}

	sysUserDao.updateByOps(sysUser);
	log.info("修改用户:{}", sysUser);
	return sysUser;
}