1.拦截器配置
Shiro默认提供了13个拦截器
public enum DefaultFilter {
anon(AnonymousFilter.class),
authc(FormAuthenticationFilter.class),
authcBasic(BasicHttpAuthenticationFilter.class),
authcBearer(BearerHttpAuthenticationFilter.class),
logout(LogoutFilter.class),
noSessionCreation(NoSessionCreationFilter.class),
perms(PermissionsAuthorizationFilter.class),
port(PortFilter.class),
rest(HttpMethodPermissionFilter.class),
roles(RolesAuthorizationFilter.class),
ssl(SslFilter.class),
user(UserFilter.class),
invalidRequest(InvalidRequestFilter.class);
}
使用时,需要自己重写一个拦截器实现自己的拦截方式,本项目使用了Jwt进行拦截,所以重写一个JwtFilter,里面重写3个方法:
isAccessAllowed 登录验证
executeLogin 具体执行方法
preHandle 访问前处理
2.路径配置
然后是对拦截路径进行配置,Shiro拦截器初始化是在ShiroFilterFactoryBean这个工厂bean中实现,
* @see org.springframework.web.filter.DelegatingFilterProxy DelegatingFilterProxy
* @since 1.0
public class ShiroFilterFactoryBean implements FactoryBean, BeanPostProcessor
所以在项目中需编写一个ShiroConfig配置类,注入这个工厂bean,本项目中代码简写如下,
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
//构造一个工厂实例
ShiroFilterFactoryBean shiroFilterFactoryBean =new ShiroFilterFactoryBean();
//设置安全处理器
shiroFilterFactoryBean.setSecurityManager(securityManager);
// 拦截器
Map filterChainDefinitionMap =new LinkedHashMap();
//排除配置文件中不需要过滤的url
if(oConvertUtils.isNotEmpty(excludeUrls)){
String[] permissionUrl =excludeUrls.split(",");
for(String url : permissionUrl){
filterChainDefinitionMap.put(url,"anon");
}
}
// 配置不会被拦截的链接 顺序判断
filterChainDefinitionMap.put("/sys/cas/client/validateLogin", "anon"); //cas验证登录
filterChainDefinitionMap.put("/sys/randomImage/