SpringBoot 集成shiro后一直拦截静态资源问题
2018-07-17 本文已影响6人
LazyMax
前言
刚做完一个项目,然后回头重新重构之前的一个框架,之前重构了一半,然后最近对SpringBoot非常感兴趣,所以想将框架与SpringBoot整合,简化配置,结果各种被拦截
环境
shiro:1.3.2
配置
package cn.ims.configuration;
import cn.ims.common.shiro.CustomRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfiguration {
//将自己的验证方式加入容器
@Bean
public CustomRealm myShiroRealm() {
return new CustomRealm();
}
//权限管理,配置主要是Realm的管理认证
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
return securityManager;
}
//Filter工厂,设置对应的过滤条件和跳转条件
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String,String> map = new LinkedHashMap<String, String>();
//不过滤
map.put("/images/**","anon");
map.put("/js/**","anon");
map.put("/css/**","anon");
map.put("/lib/**","anon");
map.put("/fonts/**","anon");
map.put("/icons/** ","anon");
map.put("/page/home/**","anon");
map.put("/page/login/**","anon");
map.put("/page/register/**","anon");
map.put("/page/404/**","anon");
map.put("/loginController/login","anon");
map.put("/","anon");
//对所有用户认证
map.put("/**","authc");
//登出
map.put("/logout","logout");
//登录
shiroFilterFactoryBean.setLoginUrl("/page/login/login.html");
//首页
//shiroFilterFactoryBean.setSuccessUrl("/index");
//错误页面,认证不通过跳转
//shiroFilterFactoryBean.setUnauthorizedUrl("/error");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
return shiroFilterFactoryBean;
}
//加入注解的使用,不加入这个注解不生效
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
}
坑
这里有2个坑:
- SecurityManager 有两个 一个是包 java.lang 中的,一个是 org.apache.shiro.mgt 中的
- Map<String,String> map = new LinkedHashMap<String, String>(); 网上很多教程都是用的HashMap 看源码后才发现应该用LinkedHashMap,否则会出现资源只能加载一次然后就被拦截的情况