文章目录
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)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下:
- 首先在登录页面选中RememberMe然后登录成功;如果是浏览器登录,一般会把RememberMe的Cookie写到客户端并保存下来;
- 关闭浏览器再重新打开;会发现浏览器还是记住你的;
- 访问一般的网页服务器端还是知道你是谁,且能正常访问;
注意:如果我们在认证的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>