Store the userId with Spring Security Authentication

后端 未结 2 462
庸人自扰
庸人自扰 2021-02-06 17:31

I need to get the userId when the authentication is loading the login, so that I can store it and use it later to gather more information about the by its ID.

Here is my

2条回答
  •  感情败类
    2021-02-06 17:59

    SecurityContextHolder.getContext().setAuthentication(result); will put the authentication object in SecurityContext which itself maintained in session if the application is a web application.

    Instead of storing the username in session you can retrieve the Authentication object using the following code.

    SecurityContext securityContext = SecurityContextHolder.getContext();
    Object principal;
    String username;
    if(null != securityContext.getAuthentication()){
       principal = securityContext.getAuthentication().getPrincipal();
       username = securityContext.getAuthentication().getName();
    }
    

    Value of username will be the username used in authentication. Value of principal will be the principal object. Many of the authentication providers will create a UserDetails object as the principal.

    Update:

    If you want to store additional information you can extend org.springframework.security.core.userdetails.User and have the additional informations as properties of that class.

    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    
    import java.util.Collection;
    
    public class CustomUser extends User {
    
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public CustomUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities,int id) {
            super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
            setId(id);
        }
    }
    

    And in loadUserByUsername return CustomUser instead of User.

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    
        empsuite.model.UserData domainUser = userloginDAO.getUsername(username);
    
        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;
    
        return new CustomUser(
                domainUser.getUsername(),
                domainUser.getPassword(),
                enabled,
                accountNonExpired,
                credentialsNonExpired,
                accountNonLocked,
                getAuthorities(1),
                domainUser.getId());
    
    }
    

    Now securityContext.getAuthentication().getPrincipal() will return CustomUser object. So you can get the ID by ((CustomUser)securityContext.getAuthentication().getPrincipal()).getId()

    SecurityContext securityContext = SecurityContextHolder.getContext();
    CustomUser user;
    if(null != securityContext.getAuthentication()){
       user = (CustomUser) securityContext.getAuthentication().getPrincipal();
    }
    int id = user.getId();
    

提交回复
热议问题