首页>代码>spring+spring boot+mybatis+shiro+thymeleaf实现社区党务后台管理系统>/PartyAffairs/Backstage/src/main/java/com/dai/party/config/realm/BackstageShiroRealm.java
package com.dai.party.config.realm;

import com.dai.party.config.CustomCredentialsMatcher;
import com.dai.party.config.SpringBeanFactoryUtils;
import com.dai.party.pojo.Backstage.BackstageUser;
import com.dai.party.service.backstageService.BackstageUserService;
import lombok.extern.slf4j.Slf4j;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @program: partyAffairs
 * @description: 项目后台配置Shiro继承AuthenticatingRealm类
 * @author: Dai Yuanchuan
 * @create: 2019-01-11 00:24
 **/
@Slf4j
@Component(value = "ShiroRealm")
public class BackstageShiroRealm extends AuthorizingRealm {

    /**
     * 项目后台用户服务注入
     */
    @Autowired
    private BackstageUserService backstageUserService;

    /**
     * 这是授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = principalCollection.getPrimaryPrincipal().toString();
        log.info("============================  username:" + username + "  ============================");
        return null;
    }

    /**
     * 这是认证方法
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.info("doGetAuthenticationInfo------------------登录------------------");
        // 手动注入服务
        if (backstageUserService == null) {
            backstageUserService = SpringBeanFactoryUtils.getBean(BackstageUserService.class);
        }
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        BackstageUser backstageUser = backstageUserService.lookupToUserName(upToken.getUsername());
        if (backstageUser == null) {
            /* 抛出用户名不存在的异常 */
            throw new UnknownAccountException();
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                backstageUser.getUserName(), backstageUser.getPassword(), getName());
        return simpleAuthenticationInfo;
    }

    /**
     * 这是重写加密规则
     */
    @PostConstruct
    public void initCredentialsMatcher() {
        setCredentialsMatcher(new CustomCredentialsMatcher());
    }
}
最近下载更多
小温ggggg  LV6 2023年10月24日
爱吃鱼的猫Vital  LV6 2023年7月31日
suyuquan  LV2 2023年7月31日
happyMrLi  LV5 2023年7月31日
yunYUN123  LV1 2023年2月26日
taowufeng2  LV8 2022年12月9日
翰昌之杰2  LV6 2022年11月30日
1025490081  LV2 2022年11月9日
wylwylqq  LV6 2022年11月6日
wuying8208  LV15 2022年10月31日
最近浏览更多
刘孟飞  LV19 4月14日
泓鼎168  LV19 3月27日
wbw123  LV5 3月10日
try8023  LV18 1月22日
廖业贵  LV18 1月5日
WBelong  LV7 2023年12月27日
hhh12345  LV7 2023年12月23日
可乐123123  LV15 2023年12月20日
小芳同学  LV1 2023年12月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友