Spring相关之SpringSecurity使用(三)

AuthenticationProvider

package com.lee.security.springsecurity;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.*;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 *
 * <p>
 * 登录认证
 */
public class MyAuthentiationProvider implements AuthenticationProvider {

    public static final Logger logger = Logger.getLogger(MyAuthentiationProvider.class);

    //SpringSecurity默认回去找UserDetailsService的实现类
    //AuthenticationProvider中的authenticate需要获取到用户验证信息
    //他是一个实现UserDetails的信息类 在spring中已经帮我们提供了线程的接口UserDetailsService以及一个默认方法loadUserByUsername
    //我们直接实现就好 如果系统存在多种认证方式 例如 手机验证码 身份证等 我们添加相关的方法即可 或者配置多个Provider即可
    //后台接收到的所有用户验证信息都可以从usernamePasswordAuthenticationToken里面拿到的
    //usernamePasswordAuthenticationToken是spring对AbstractAuthenticationToken的默认实现 当然根据业务不同我们也可以自己实现
    //这里我们用到的是MyAuthenticationToken来封装
    @Autowired
    private UserDetailsService springSecurityUserService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //将authentication转成我们的MyAuthenticationToken
        MyAuthenticationToken authenticationToken = (MyAuthenticationToken) authentication;
        //可以从里面拿到各种用户信息
        String rrtn = authenticationToken.getUsername();
        String phone = authenticationToken.getPhone();
        String idCard = authenticationToken.getIdCard();
        String code = authenticationToken.getCode();
        //根据拿到的参数 去使用不同的验证方法 例如 我们看到了phone 和 code的参数合法
        //我们就可以使用UserDetailsService中我们自己添加的方法loadUserByPhoneCode
        //用户名 密码 合法我们就可以使用 loadUserByUsername
        SpringSecurityUserInfo springSecurityUserInfo= (SpringSecurityUserInfo) springSecurityUserService.loadUserByUsername(rrtn);
        //根据用户信息状态 来决定是否可以登录
        //BadCredentialsException 坏的凭据
        //AccountExpiredException 账户过期
        //LockedException 账户锁定
        //DisabledException 账户不可用
        //CredentialsExpiredException 证书过期
        //等等 这些异常都会交由登录失败处理器 来处理
//        if(springSecurityUserInfo.isAccountNonExpired()){
//            throw new AccountExpiredException("AccountExpiredException");
//        }
//        if(springSecurityUserInfo.isAccountNonLocked()){
//            throw new LockedException("LockedException");
//        }
//        if(springSecurityUserInfo.isCredentialsNonExpired()){
//            throw new CredentialsExpiredException("BadCredentialsException");
//        }
//        if(springSecurityUserInfo.isEnabled()){
//            throw new DisabledException("DisabledException");
//        }
        //将得到的用户信息装填到MyAuthenticationToken中并返回
        authenticationToken.setSpringSecurityUserInfo(springSecurityUserInfo);
        logger.info("MyAuthentiationProvider:MyAuthenticationToken:"+authenticationToken);
        return authenticationToken;
    }

    //装填自己实现的AbstractAuthenticationToken
    @Override
    public boolean supports(Class<?> authentication) {
        return MyAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。