spring boot + shiro静态资源放行
Shiro配置类配置
按照常规流程,在ShiroConfigration(配置类,自定义类名)中放行静态资源
下面展示一些 内联代码片
。
// An highlighted block
Map<String,String> filterMap = new LinkedHashMap<>();
//根目录不拦截
// filterMap.put("/","anon");
//登录页面不拦截
filterMap.put("/login.html","anon");
//登录请求不拦截
filterMap.put("/demo/login","anon");
//登录请求不拦截
filterMap.put("/user/login","anon");
//主页面请求不拦截
filterMap.put("/demo/index","anon");
//静态资源不拦截
filterMap.put("/static/**","anon");
//其他请求页面全部拦截
filterMap.put("/**","authc");
//设置拦截
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
//设置拦截后返回请求
shiroFilterFactoryBean.setLoginUrl("/demo/login");
//未授权的返回请求
shiroFilterFactoryBean.setUnauthorizedUrl("/demo/login");
这时候运行项目,一般就ok了,但是有的小伙伴还是发现静态资源被拦截的问题。这就需要排查问题了
问题排查-静态资源是否真的放行了?
这里先一步步排查问题,确定静态资源是否真的放行了。在网页打开静态资源的链接,比如我这里静态资源有:localhost:8080/css/public.css
如果是以上的画面,那么恭喜你,静态资源是放行了的,问题出在html页面的加载上,怎么解决,这里先卖个关子。
静态资源没有放行
如果是没有放行,要么界面加载是经典的springboot找不到资源的界面,要么就是跳转到你设置的login页面。
这里还需要详细的放行静态资源,比如详细放行到具体文件夹。
// An highlighted block
filterMap.put("/static/**","anon");
filterMap.put("/css/**","anon");
filterMap.put("/js/**","anon");
filterMap.put("/layui/**","anon");
filterMap.put("/json/**","anon");
filterMap.put("/images/**","anon");
//其他请求页面全部拦截
filterMap.put("/**","authc");
//设置拦截
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
之后仍可通过上面说过的直接通过页面加载静态资源文件的方法测试是否真的放行了静态资源。
静态资源放行了,页面仍然不加载
这里就是一个小小的bug了,前端的界面大部分是其他编辑器上copy过来的,本来其他地方能够运行的,这里一般不会想到是页面加载资源的问题。解决方法如下
其实就是在引入资源的路径前加‘/’
//原本的资源引入
<script type="text/javascript" src="layui/layui.js"></script>
//更改之后,亲测有效
<script type="text/javascript" src="/layui/layui.js"></script>
//css样式也一样
<link rel="stylesheet" href="/css/public.css" media="all" />