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 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(); } }