shiro教程(session和remember me)

本文详细介绍了Apache Shiro框架中的Session管理和Remember Me功能。通过自定义FormAuthenticationFilter实现登录成功后将用户信息保存到Session中,并配置Shiro的applicationContext-shiro.xml确保RememberMe生效。测试验证了Remember Me功能,即使关闭浏览器,再次访问仍能记住用户身份。此外,文章还涵盖了相关配置文件如shiro-ehcache.xml、springmvc.xml和web.xml的设置,以及涉及的jsp、pojo、mapper、service、controller和filter等内容。
摘要由CSDN通过智能技术生成

session

shiro提供的session不依赖web容器,可以直接使用,如果是在web环境下,session中的数据和httpsession中的数据是通的。Shiro中的session可以出现在任何地方,例如service、dao等,不需要从controller中传递session参数,用户保存在session中的数据可以在HTTP session中获取,保存在httpsession中的数据也可以从session中获取。

session常用方法

在这里插入图片描述
在这里插入图片描述

实现登录成功后保存登录信息到session中

创建FormAuthenticationFilter的子类重写onLoginSuccess方法

package com.sxt.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
/**
 * 自定义的FormAuthenticationFilter过滤器
 * @author xieS
 *
 */
public class MyFormAuthenticationFilter extends FormAuthenticationFilter{
   

	/**
	 * 当登录成功的时候执行的方法
	 */
	@Override
	protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
			ServletResponse response) throws Exception {
   
		// 向session中保存信息
		Session session = subject.getSession();
		session.setAttribute("msg", "登录成功了。。。");
		return super.onLoginSuccess(token, subject, request, response);
	}
}

配置文件中配置(applicationContext-shiro.xml)

<!-- 配置自定义的表单过滤器 -->
<bean id="customFormAuthenticationFilter" class="com.dpb.filter.CustomFormAuthenticationFilter">
		<!-- 可以修改表单接收的参数名称 -->
		<property name="usernameParam" value="username" />
		<property name="passwordParam" value="password" />
	</bean>

	<!-- 注册ShiroFilterFactoryBean 注意id必须和web.xml中注册的targetBeanName的值一致 -->
	<bean class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"
		id="shiro">
		<!-- 注册SecurityManager -->
		<property name="securityManager" ref="securityManager" />
		<!-- 登录地址 如果用户请求的的地址是 login.do 那么会对该地址认证 -->
		<property name="loginUrl" value="/login.do" />
		<!-- 登录成功的跳转地址 -->
		<property name="successUrl" value="/jsp/success.jsp" />
		<!-- 访问未授权的页面跳转的地址 -->
		<property name="unauthorizedUrl" value="/jsp/refuse.jsp" />

		<property name="filters">
			<map>
				<entry key="authc" value-ref="customFormAuthenticationFilter"/>
			</map>
		</property>
		<!-- 设置 过滤器链 -->
		<property name="filterChainDefinitions">
			<value>
			<!--加载顺序从上往下。
 					authc需要认证
 					anon可以匿名访问的资源
 				 -->
				/login.do=authc
				/login.jsp=anon
				/logout=logout
				/**=user
			</value>
		</property>
	</bean>

注意:必须配置为 user级别,authc级别的rememberMe没有效果
在这里插入图片描述

测试在这里插入图片描述

在这里插入图片描述
 到此就可以测试了。登录的时候勾选记住密码关闭浏览器,再访问user级别的请求就直接可以访问了。但是因为此时并没有真正的认证,所以此时的session并不能使用,这时我们可以实现一个过滤器。来拦截rememberMe功能的请求即可

remember me

Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下:

  1. 首先在登录页面选中RememberMe然后登录成功;如果是浏览器登录,一般会把RememberMe的Cookie写到客户端并保存下来;
  2. 关闭浏览器再重新打开;会发现浏览器还是记住你的;
  3. 访问一般的网页服务器端还是知道你是谁,且能正常访问;

在这里插入图片描述
在这里插入图片描述
 注意:如果我们在认证的AuthenticationInfo info = new SimpleAuthenticationInfo(user, pwd, credentialsSalt, “myrealm”); 保存的是自定义的对象,那么该对象必须实现Serializable接口,因为该对象要被持久化到cookie中!!!

登录表单中添加记住我复选框

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>${msg}</h1>
	<form action="/login.do" method="post">
		账号:<input type="text" name="username" value="zhangsan"><br>
		密码:<input type="password" name="password" value="123456"><br>
		<input type="checkbox" name="rememberMe"/>记住密码<br/>
		<input type="submit" value="登录">
	</form>
</body>
</html>

jsp页面

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录失败</h1>
</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 引入shiro标签 -->
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>shiro权限标签:</h1>
	<h2>
		当前登录用户:<shiro:principal property="username"></shiro:principal>
	</h2>
	<!-- 只有没登录才能看到 -->
	<shiro:guest>
    	<label>您当前是游客,</label><a href="/login.jsp" >请登录</a>
	</shiro:guest>
	<!-- 只有验证通过才能看到 -->
	<shiro:authenticated>
    	<label>用户身份验证已通过 </label>
	</shiro:authenticated>
	<ul>
		<!-- 需要有query权限 -->
		<shiro:hasPermission name="query">
			<li><a href="#">用户管理-查看</a></li>
		</shiro:hasPermission>
		<shiro:hasPermission name="insert">
			<li><a href="#">用户管理-添加</a></li>
		</shiro:hasPermission>
		<shiro:hasPermission name="delete">
			<li><a href="#">用户管理-删除</a></li>
		</shiro:hasPermission>
		<!-- 登录就可以访问 -->
		<shiro:authenticated>
			<li><a href="#">用户管理-更新</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值