Log user in with remember-me functionality in Spring 3.1

后端 未结 2 780
南方客
南方客 2020-12-13 11:04

I currently log users in programmatically (like when they login through Facebook or other means than using my login form) with:

SecurityContextHolder.getCont         


        
2条回答
  •  无人及你
    2020-12-13 11:13

    This is the source for the constructor.

    public RememberMeAuthenticationToken(String key, Object principal, Collection authorities) {
        super(authorities);
    
        if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal)) {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    
        this.keyHash = key.hashCode();
        this.principal = principal;
        setAuthenticated(true);
    }
    

    The key is hashed and its used to determine whether the authentication used for this user in the security context is not a 'forged' one.

    Have a look at the RememberMeAuthenicationProvider source.

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if (!supports(authentication.getClass())) {
            return null;
        }
    
        if (this.key.hashCode() != ((RememberMeAuthenticationToken) authentication).getKeyHash()) {
            throw new BadCredentialsException(messages.getMessage("RememberMeAuthenticationProvider.incorrectKey",
                    "The presented RememberMeAuthenticationToken does not contain the expected key"));
        }
    
        return authentication;
    }
    

    So to answer your question, you need to pass the hash code of the key field of the Authentication representing the user.

提交回复
热议问题