一、使用
1.搭建基础环境
(1)导入 Spring 和 Shiro 的 Jar 包
- 正常导入 spring jar包
- 导入日志包
- log4j-1.2.15.jar
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
- 导入 shiro 包
- shiro-core-1.2.2.jar
- shiro-ehcache-1.2.2.jar
- shiro-spring-1.2.2.jar
- shiro-web-1.2.2.jar
(2)配置文件
- web.xml
- 读取所有配置文件
- springmvc 的 DispatcherServlet 配置
- shiroFilter 的配置(该配置参考的是:shiro-root-1.2.2\samples\spring\src\main\webapp\WEB-INF\web.xml)
- 见文章末的两个 web.xml
- spring-shiro.xml
- 该配置参考:shiro-root-1.2.2\samples\spring\src\main\webapp\WEB-INF\applicationContext.xml
- 此次实验在 shiro.xml 文件中采用的缓存管理器是 ehcache 。需要额外导入ehcache jar包和配置文件。
- ehcache 使用的 Hibernate 下的。
- ehcache-core-2.4.3.jar(hibernate-release-4.2.4.Final\lib\optional\ehcache\) 和 ehcache.xml(hibernate-release-4.2.4.Final\project\etc\)
- 见文章末的两个 spring-shiro.xml
- 注意:(1)缓存的配置(2)自定义Realm
- spring.xml 正常配置即可。
- springmvc.xml 正常配置即可。
(3)检测
- 添加自定义 Realm,需要继承自 AuthorizingRealm(即包含认证,也包含授权的 Realm)
- 添加了一个空的自定义的 Realm ,没有添加认证和授权逻辑。
- 启动项目,检测能否正常启动,检测配置是否正确。
2.登录
(1)添加登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4> Login Page </h4> <form action="shiro-login" method="post"> <input name="userName" type="text"/> <input name="password" type="password"/> <input type="submit" value="submit"> </form> </body> </html>