liusheng
2023-06-28 993579dcbc5d87755ff8dc0cc95f53a50387b94c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.ruoyi.framework.wxopenidConfig;
 
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
 
import java.util.Collection;
 
/**
 * 微信OpenID登录 AuthenticationToken,模仿 UsernamePasswordAuthenticationToken 实现
 */
public class WxOpenIDAuthenticationToken extends AbstractAuthenticationToken {
 
    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
 
    /**
     * 在 UsernamePasswordAuthenticationToken 中该字段代表登录的用户名,
     * 在这里就代表登录的微信OpenID
     */
    private final Object principal;
 
    /**
     * 构建一个没有鉴权的 WxOpenIDAuthenticationToken
     */
    public WxOpenIDAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        setAuthenticated(false);
    }
 
    /**
     * 构建拥有鉴权的 WxOpenIDAuthenticationToken
     */
    public WxOpenIDAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        // must use super, as we override
        super.setAuthenticated(true);
    }
 
    @Override
    public Object getCredentials() {
        return null;
    }
 
    @Override
    public Object getPrincipal() {
        return this.principal;
    }
 
    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
 
        super.setAuthenticated(false);
    }
 
    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}