模仿spring authentication-provider 自己写登录人管理

    
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
            
//将code交给spring管理
String openId="sssss";    
//将传过来的openId封装成
Authentication
Authentication token = new UsernamePasswordAuthenticationToken(openId, ""); 
//调用AuthenticationManager的实现类,验证后返回
Authentication authenticate
= new WechatAuthentication().authenticate(token);
//将返回结果set到ContextHolder管理
SecurityContextHolder.getContext().setAuthentication(authenticate);

//获取方式 Authentication authentication
= SecurityContextHolder.getContext().getAuthentication();



import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationException;
import org.springframework.security.AuthenticationManager;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

/**
 * AuthenticationManager实现类
 * @author yanglei
 * 2016-6-17
 */
public class WechatAuthentication implements AuthenticationManager{
    
    @Override
    public Authentication authenticate(Authentication arg0)
            throws AuthenticationException {
        return new UsernamePasswordAuthenticationToken(arg0.getName(),"");
    }
}


原文地址:https://www.cnblogs.com/kisstear/p/5593821.html