首页>代码>基于springboot+layui实现的医院信息管理系统>/src/main/java/com/aaa/config/ShiroConfig.java
package com.aaa.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    /**
     * 第一个bean:realm  数据域
     * <p>
     * 第二个bean:SecurityManager 安全管理器
     * <p>
     * 第三个bean:shirofileterfactorybean
     * <p>
     * 第四个:密码匹配器
     * <p>
     * 第五个bean:ShiroDialect
     */

    // <bean id="">   name对应bean容器的id
    @Bean(name = "userRealm")
    public UserRealm getUserRealm(@Qualifier("hashedCredentialsMatcher") HashedCredentialsMatcher hashedCredentialsMatcher) {
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean(name = "shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String, String> filtermap = new LinkedHashMap<String, String>();
        //toLogin/login界面可以未认证直接访问


        filtermap.put("/sel/login", "anon");
        filtermap.put("index.html", "anon");


        filtermap.put("/css/**", "anon");
        filtermap.put("/layui/**", "anon");
        filtermap.put("/js/**", "anon");
        filtermap.put("/images/**", "anon");
        filtermap.put("*.png", "anon");
        filtermap.put("*jpg", "anon");



        //未认证都不允许通过
        filtermap.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filtermap);
        //如果访问的页面未认证   跳转到登陆页面
        shiroFilterFactoryBean.setLoginUrl("/sel/toLogin");

        return shiroFilterFactoryBean;
    }

    //用来跟加密的密码进行比对的bean

    @Bean(name = "hashedCredentialsMatcher")
    public HashedCredentialsMatcher getHashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置比较规则
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //迭代2次
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

    //shiro 与thymeleaf的整合
    @Bean
    public ShiroDialect getShiroDialect() {
        return new ShiroDialect();
    }
}
最近下载更多
zhaopb  LV8 5月17日
zhaorrr  LV2 2024年11月7日
yyhrhv  LV8 2024年8月31日
叁卍石  LV5 2024年8月21日
TY0165  LV20 2024年6月21日
pxc1994  LV9 2024年5月14日
微信网友_6927932988952576  LV12 2024年3月31日
提灯寻白鹿  LV3 2024年3月27日
qqttuu  LV3 2024年3月4日
Hopu2024  LV2 2024年2月22日
最近浏览更多
2225906816 5月26日
暂无贡献等级
zxdfdcfffddssd 5月21日
暂无贡献等级
123456789x  LV1 5月19日
zhaopb  LV8 5月17日
TaoTaot 4月7日
暂无贡献等级
小宋敲代码 3月25日
暂无贡献等级
gxyhk666 3月25日
暂无贡献等级
blanknb 3月18日
暂无贡献等级
尼可曲奇 3月3日
暂无贡献等级
yvimib  LV3 3月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友