springboor集成shiro不得不说遇到的大坑,最近想要练习一下shiro的安全框架。
发现在原来项目基础之上加上了shiro之后,原来的html页面访问报404,百思不得其解。本以为是自己的代码写错了,或者是哪里出现了问题。经过在网上翻阅询问之后,发现竟然是自己的shiro框架搞的鬼,被自己的shiro框架给拦截了。
下面我只将自己的shiroConfig配置文件展现给大家,因为该框架还没有完善,以后我将陆续的一一为大家呈现。、
package com.zmj.config;
import com.zmj.realm.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* 创建一个shiro的配置类
*/
@Configuration
public class ShiroConfig {
/**
* 创建一个过滤器
*/
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 设置过滤器链 设置过滤规则
/**
* anon 代表不过滤 放行
* authc 代表过滤 认证
* 放行优先于认证
*/
Map map=new HashMap();
// map.put("**/login.html","anon");
map.put("/**/login.html","anon");
map.put("/login","anon");
map.put("/regist.jsp","anon");
map.put("/regist","anon");
map.put("/css/**","anon");
map.put("/audio/**","anon");
map.put("/img/**","anon");
map.put("/js/**","anon");
map.put("/json/**","anon");
map.put("/script/**","anon");
map.put("/themes/**","anon");
map.put("/**","authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
shiroFilterFactoryBean.setSecurityManager(securityManager);
return shiroFilterFactoryBean;
}
@Bean
public DefaultWebSecurityManager getSecurityManager(MyRealm myRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 设置自定义realm
securityManager.setRealm(myRealm);
return securityManager;
}
/**
* 创建自定义realm
*/
@Bean
public MyRealm getRealm(CredentialsMatcher credentialsMatcher){
MyRealm myRealm = new MyRealm();
// 加密处理 设置密码处理凭证器
myRealm.setCredentialsMatcher(credentialsMatcher);
return myRealm;
}
/**
* 设置密码凭证器 对密码进行1024次解密
*/
@Bean
public CredentialsMatcher getCredentialsMatcher(){
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//设置算法名字
credentialsMatcher.setHashAlgorithmName("md5");
//设置散列次数
credentialsMatcher.setHashIterations(1024);
return credentialsMatcher;
}
/**
* 开启shiro aop注解支持.
* 使用代理方式;所以需要开启代码支持;否则@RequiresRoles等注解无法生效
* @param securityManager
* @return
*/
/*@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}*/
}
值的注意的就是我截图给大家的这一点,表示shiro的放行规则,anon表示放行,authc表示不放行(及表示登录之后才会允许访问),注意:我们一般将放行的写在上面,最后将所有的都列为不放行,写在最后。自我感觉还是为了安全
我的map集合中,第一个put中放入了login.html,及表示放行,所以我们就能够加载出该html页面。这也就是我为什么刚开始所说的为什么整合shiro过后访问静态资源出现404状况,这是被自己拦截了啊!!!
这辈子坚持与不坚持都不可怕,可怕的是独自走在坚持的道路上!!!
欢迎加入技术群聊!