Java Code Examples for org.springframework.security.core.context.SecurityContext#getAuthentication()

The following examples show how to use org.springframework.security.core.context.SecurityContext#getAuthentication() . 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: SecurityUtils.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Get the login of the current user.
 *
 * @return the login of the current user
 */
public static String getCurrentUserLogin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    String userName = null;
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof UserDetails) {
            UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
            userName = springSecurityUser.getUsername();
        } else if (authentication.getPrincipal() instanceof String) {
            userName = (String) authentication.getPrincipal();
        }
    }
    return userName;
}
 
Example 2
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 3
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 4
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 5
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 6
Source File: UmsMemberServiceImpl.java    From macrozheng with Apache License 2.0 5 votes vote down vote up
@Override
public UmsMember getCurrentMember() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
    return memberDetails.getUmsMember();
}
 
Example 7
Source File: UmsMemberServiceImpl.java    From mall-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public UmsMember getCurrentMember() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
    return memberDetails.getUmsMember();
}
 
Example 8
Source File: SecurityUtils.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
/**
 * If the current user has a specific authority (security role).
 * <p>
 * The name of this method comes from the isUserInRole() method in the Servlet API
 *
 * @param authority the authority to check
 * @return true if the current user has the authority, false otherwise
 */
public static boolean isCurrentUserInRole(String authority) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null) {
        return authentication.getAuthorities().stream()
            .anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(authority));
    }
    return false;
}
 
Example 9
Source File: SecurityUtils.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a user is authenticated.
 *
 * @return true if the user is authenticated, false otherwise
 */
public static boolean isAuthenticated() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null) {
        return authentication.getAuthorities().stream()
            .noneMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS));
    }
    return false;
}
 
Example 10
Source File: SecurityUtils.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Get the JWT of the current user.
 *
 * @return the JWT of the current user
 */
public static String getCurrentUserJWT() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null && authentication.getCredentials() instanceof String) {
        return (String) authentication.getCredentials();
    }
    return null;
}
 
Example 11
Source File: UmsMemberServiceImpl.java    From xmall with MIT License 5 votes vote down vote up
@Override
public UmsMember getCurrentMember() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
    return memberDetails.getUmsMember();
}
 
Example 12
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 13
Source File: UserFeignClientInterceptor.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(RequestTemplate template) {

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

    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {

        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
    }
}
 
Example 14
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static int getUserId(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user.getId();
}
 
Example 15
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static int getUserId(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user.getId();
}
 
Example 16
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static TUser getUserDetail(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user;
}
 
Example 17
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static int getUserId(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user.getId();
}
 
Example 18
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static TUser getUserDetail(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user;
}
 
Example 19
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static int getUserId(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user.getId();
}
 
Example 20
Source File: UserHolder.java    From spring-security with Apache License 2.0 4 votes vote down vote up
public static TUser getUserDetail(){
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    TUser user = (TUser) auth.getPrincipal();
    return user;
}