UserRealm.java 3.18 KB
package com.lhcredit.framework.shiro.realm;

import com.lhcredit.common.exception.user.*;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.framework.shiro.service.WebLoginService;
import com.lhcredit.framework.shiro.token.CustomizedToken;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * 自定义Realm 处理登录 权限,用户app用户登录
 */
public class UserRealm extends AuthorizingRealm {
    private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
    // 对外接口Session超时时间(默认一天)
    @Value("${shiro.session.webExpireTime}")
    private int webExpireTime;

    @Autowired
    private WebLoginService webLoginService;

    /**
     * 用户登录app不加载权限
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
       return null;
    }

    /**
     * 用户登录app认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        CustomizedToken upToken = (CustomizedToken) token;
        String username = upToken.getUsername();
        String password = "";
        if (upToken.getPassword() != null) {
            password = new String(upToken.getPassword());
        }
        FrontUser frontUser = null;
        try {
//            frontUser = webLoginService.login(username, password);
        } catch (CaptchaException e) {
            throw new AuthenticationException(e.getMessage(), e);
        } catch (UserNotExistsException e) {
            throw new UnknownAccountException(e.getMessage(), e);
        } catch (UserPasswordNotMatchException e) {
            throw new IncorrectCredentialsException(e.getMessage(), e);
        } catch (UserPasswordRetryLimitExceedException e) {
            throw new ExcessiveAttemptsException(e.getMessage(), e);
        } catch (UserBlockedException e) {
            throw new LockedAccountException(e.getMessage(), e);
        } catch (RoleBlockedException e) {
            throw new LockedAccountException(e.getMessage(), e);
        } catch (Exception e) {
            log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
            throw new AuthenticationException(e.getMessage(), e);
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(frontUser, password, getName());
        //LGJ 超时时间单位为毫秒,优先级大于shiroConfig中GlobalSessionTimeout全局超时时间
        ShiroUtils.getSubject().getSession().setTimeout(webExpireTime * 60 * 1000);
        return info;
    }

    /**
     * 清理缓存权限
     */
    public void clearCachedAuthorizationInfo() {
        this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
    }
}