package cn.estsh.i3plus.core.apiservice.auth.realm;

import cn.estsh.i3plus.core.api.iservice.base.ISystemLoginService;
import cn.estsh.i3plus.pojo.base.shirotoken.UserToken;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
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.stereotype.Component;

/**
 * @Description : 验证规则
 * @Reference :-
 * @Author : alwaysfrin
 * @CreateDate : 2018-10-13 14:04
 * @Modify:
 **/
@Component
public class UserAuthRealm extends AuthorizingRealm {
    public static final Logger LOGGER = LoggerFactory.getLogger(UserAuthRealm.class);

    @Autowired
    private ISystemLoginService systemLoginService;

    public UserAuthRealm(){
        //添加支持的token
        this.setAuthenticationTokenClass(UserToken.class);
    }

    //权限验证
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return new SimpleAuthorizationInfo();
    }

    //令牌确认
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //管理员令牌
        SessionUser sessionUser = systemLoginService.queryCheckUserLogin((UserToken) authenticationToken);
        LOGGER.info("{}-用户令牌验证：{}",this.getName(),sessionUser);
        return new SimpleAuthenticationInfo(sessionUser,((UserToken) authenticationToken).getLoginPwd(),this.getName());
    }
}
