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

import cn.estsh.i3plus.core.api.iservice.base.ISystemLoginService;
import cn.estsh.i3plus.pojo.base.shirotoken.AdminToken;
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;

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

    @Autowired
    private ISystemLoginService systemLoginService;

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

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

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