當前位置: 首頁>>代碼示例>>Java>>正文


Java OAuth2Authentication.getPrincipal方法代碼示例

本文整理匯總了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;
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:26,代碼來源:DomainTokenServices.java

示例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;
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:26,代碼來源:DomainTokenServices.java

示例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;
}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:17,代碼來源:AdditionalClaimsTokenEnhancer.java

示例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;
}
 
開發者ID:tadeucruz,項目名稱:spring-oauth2-jwt,代碼行數:18,代碼來源:JWTTokenEnhancer.java

示例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;
}
 
開發者ID:xm-online,項目名稱:xm-ms-entity,代碼行數:27,代碼來源:TenantInterceptor.java

示例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);
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:12,代碼來源:DomainTokenServices.java

示例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");
    }
}
 
開發者ID:SopraSteriaGroup,項目名稱:initiatives_backend_auth,代碼行數:9,代碼來源:CustomAccessTokenConverter.java

示例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);
}
 
開發者ID:tadeucruz,項目名稱:spring-oauth2-jwt,代碼行數:10,代碼來源:EndPoint.java

示例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);
}
 
開發者ID:tadeucruz,項目名稱:spring-oauth2-jwt,代碼行數:10,代碼來源:EndPoint.java

示例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;
}
 
開發者ID:tinmegali,項目名稱:Using-Spring-Oauth2-to-secure-REST,代碼行數:13,代碼來源:AuthorizationConfig.java

示例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;
}
 
開發者ID:xm-online,項目名稱:xm-ms-config,代碼行數:34,代碼來源:TenantInterceptor.java

示例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;
}
 
開發者ID:ustcwudi,項目名稱:springboot-seed,代碼行數:12,代碼來源:CustomTokenEnhancer.java

示例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;
}
 
開發者ID:bndynet,項目名稱:web-framework-for-java,代碼行數:15,代碼來源:OAuth2Config.java

示例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;
    }
}
 
開發者ID:xm-online,項目名稱:xm-ms-balance,代碼行數:68,代碼來源:TenantInterceptor.java

示例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;
    }
}
 
開發者ID:xm-online,項目名稱:xm-ms-dashboard,代碼行數:69,代碼來源:TenantInterceptor.java


注:本文中的org.springframework.security.oauth2.provider.OAuth2Authentication.getPrincipal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。