All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shawven.security.app.connect.wxmini.WxMiniAuthenticationToken Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version

package com.github.shawven.security.app.connect.wxmini;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;


public class WxMiniAuthenticationToken extends AbstractAuthenticationToken {

	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

	private final Object principal;

	private String providerId;

	/**
	 * This constructor can be safely used by any code that wishes to create a
	 * UsernamePasswordAuthenticationToken, as the {@link #isAuthenticated()}
	 * will return false.
	 */
	public WxMiniAuthenticationToken(String openId, String providerId) {
		super(null);
		this.principal = openId;
		this.providerId = providerId;
		setAuthenticated(false);
	}

	/**
	 * This constructor should only be used by AuthenticationManager or
	 * AuthenticationProvider implementations that are satisfied with
	 * producing a trusted (i.e. {@link #isAuthenticated()} = true)
	 * authentication token.
	 *
	 * @param principal
	 * @param authorities
	 */
	public WxMiniAuthenticationToken(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;
	}

	public String getProviderId() {
		return providerId;
	}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy