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(); } }
最近下载更多
qqttuu LV3
3月4日
Hopu2024 LV2
2月22日
xk16675552250 LV33
1月5日
小安同学 LV7
2023年12月30日
阿桐吖 LV2
2023年12月29日
牛Bgtp LV1
2023年12月22日
lllxxxwofhoa LV2
2023年12月10日
2010160433 LV3
2023年12月7日
lvhongquan LV11
2023年9月19日
yqyqyqyqy LV4
2023年7月23日
最近浏览更多
ggjkvyj
3月16日
暂无贡献等级
微信网友_6909455156350976 LV2
3月16日
棒棒糖123
3月14日
暂无贡献等级
miku007
3月13日
暂无贡献等级
谢谢谢谢谢谢你 LV6
2月28日
Hopu2024 LV2
2月22日
FF加菲猫 LV4
2月15日
khhuyvhgcd LV1
2月11日
ljhgff LV1
2月10日
Gin19960217 LV3
1月22日