shiro 整合 springmvc 配置静态资源过滤

一、背景

       最近在看 shiro 的过滤器,是基于springboot + spring mvc + shiro + jsp 构建的一个小工程,我想在 jsp 的页面中加载 jscss 和一些图片资源,遇到的一些小的问题,在此记录一下。

二、标签

       先看下 application.properties 里面的两个标签,下面的这个标签,只有静态资源的访问路径为为 /static/** 时,才会处理该请求,比如:访问http://localhost:8080/static/css/index.css,处理的方式是根据模式匹配后的文件名查找本地文件。按照 spring.resources.static-locations 指定的位置查找本地文件。

spring.mvc.static-path-pattern=/static/**

       再看下面这个标签,这个标签是自定义 springboot 前端静态资源的位置,如果不配置,则默认将从如下位置,按照优先级查找静态资源文件,从左向右是由高到低
spring.resources.static-locations = classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

spring.resources.static-locations = classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

三、示例工程

       我的 jsp 文件放在了 webapp 文件夹的 page 文件夹下,而我的 jsimage css 文件则放在了 resources static 文件夹下,如下所示:

       我的初始化登录界面 login.jsp 的内容如下所示,如果此时不引入 shiro ,则下面的界面可以正常显示,即可以正常的显示和加载所有的资源。

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>一路发咨询网站</title>
</head>
<body>
<script type="text/javascript" src="/static/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/js/login.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/login.css"/>
<h1>欢迎登录一路发咨询网站</h1>

<form action="/login" method="post">
	<div id="father" style="background-image: url('/static/image/index.png');">
		<div style="width:300px;height:100px;">
			<div style="width:150px;float:left">
				<span>用户名:<span></span>
			</div>
			<div style="width:150px;float:left;">
				<input  style="height:34px" type="text" name="userName"/>
			</div>
		</div>
		<div style="width:300px;height:100px;">
			<div style="width:150px;float:left;">
				<span>密码:<span></span>
			</div>
			<div style="width:150px;float:left;">
				<input  style="height:34px" type="password" name="password"/>
			</div>
		</div>
		<div style="width:300px;height:100px;">
			<div style="width:64px;float:left;margin-left:280px">
				<input style="height:34px;width:34px;" type="checkbox" name="rememberMe" />
			</div>
			 <div style="width:150px;float:left;margin-top:-4px">
					<span>记住我<span></span>
			</div>
		</div>
		 <div style="margin-left:190px">
				<input style="height:50px;width:90px;font-size:34px;font-weight:bold" type="submit" value="提交"/>
		</div>
	</div>
</form>
</body>
</html>

 四、引入shiro

       由于 shiro 拥有过滤器的功能,如果按照下面的这种方式配置,则上面的 login.jsp 里面所需要的 jscss 和图片资源都无法正常加载。

@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
		// Shiro的核心安全接口,这个属性是必须的
		shiroFilter.setSecurityManager(securityManager);		
		//身份认证失败,则跳转到登录页面的配置 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,
        //不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。
		shiroFilter.setLoginUrl("/page/login.jsp");
		//自定义过滤
		Map<String, String> map = new LinkedHashMap<>();
		// 不能对login方法进行拦截,若进行拦截的话,这辈子都登录不上去了,这个login是LoginController里面登录校验的方法
		map.put("/login", "anon");
		//对所有请求进行拦截
		map.put("/**", "authc");
		shiroFilter.setFilterChainDefinitionMap(map);
		return shiroFilter;
	}

五、解决方式

       只需要在上面的这个方法中,加一个过滤条件即可,如下所示,千万记得 map.put("/**", "authc") 这个拦截条件要放在最后。

@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
		// Shiro的核心安全接口,这个属性是必须的
		shiroFilter.setSecurityManager(securityManager);		
		//身份认证失败,则跳转到登录页面的配置 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,
        //不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。
		shiroFilter.setLoginUrl("/page/login.jsp");
		//自定义过滤
		Map<String, String> map = new LinkedHashMap<>();
		// 不能对login方法进行拦截,若进行拦截的话,这辈子都登录不上去了,这个login是LoginController里面登录校验的方法
		map.put("/login", "anon");
		map.put("/static/**", "anon");
		//对所有请求进行拦截
		map.put("/**", "authc");
		shiroFilter.setFilterChainDefinitionMap(map);
		return shiroFilter;
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值