SSM + Shiro 整合 (6)- Shiro 集成 Spring
来源:互联网 发布:男朋友 不好看 知乎 编辑:程序博客网 时间:2024/05/01 14:19
项目源码:https://github.com/weimingge14/Shiro-project
演示地址:http://liweiblog.duapp.com/Shiro-project/login
用户名:admin,密码:123456
用户名:liwei,密码:123456
用户名:huzhenyu,密码:123456
这一节将 Shiro 整合进 Spring。前面一节我们仅仅只是加入了 Shiro,并没有使用 Spring 来管理 Shiro。所以我们在自定义的 Realm 中并不能使用 Spring 的依赖注入特性。
步骤 1:加入 Shiro 集成 Spring 的依赖
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version></dependency>
步骤 2:将 shiro.ini 文件翻译成 Spring 的配置文件(xml 格式)
shiro.ini 文件
# 本项目已经使用了 shiro 集成 Spring , 该配置文件已经毫无意义# 本配置文件原来应该起到的作用配置在 classpath 路径下的 spring/shiro-web.xml 中[main]# 声明一个密码匹配器credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher# 设置该密码匹配器使用的算法是 md5credentialsMatcher.hashAlgorithmName=md5# 声明一个自定义的 RealmmyRealm = com.liwei.shiro.realm.MyRealm# 将上面声明的密码匹配器注入到自定义 Realm 的属性中去myRealm.credentialsMatcher=$credentialsMatcher# 自定义一个权限匹配器permissionResolver=com.liwei.shiro.permission.UrlPermissionResolver# 将自定义的权限匹配器注入到自定义 Realm 中myRealm.permissionResolver = $permissionResolver# 设置安全管理器的安全数据源为自定义的 RealmsecurityManager.realms=$myRealm# 如果认证不通过,浏览器通过 Get 方式请求到 /login 上authc.loginUrl=/login[filters]# 声明一个自定义的过滤器resourceCheckFilter = com.liwei.shiro.filter.ResourceCheckFilter# 为上面声明的自定义过滤器注入属性值resourceCheckFilter.errorUrl=/unAuthorization[urls]# 配置 url 与使用的过滤器之间的关系/admin/**=authc,resourceCheckFilter/login=anon
翻译好的 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 声明一个密码匹配器 --> <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <!-- 设置该密码匹配器使用的算法是 md5 --> <property name="hashAlgorithmName" value="md5"/> </bean> <!-- 声明一个自定义的 Realm --> <bean id="myRealm" class="com.liwei.shiro.realm.MyRealm"> <!-- 将上面声明的密码匹配器注入到自定义 Realm 的属性中去 --> <property name="credentialsMatcher" ref="credentialsMatcher"/> <!-- 将自定义的权限匹配器注入到自定义 Realm 中 --> <property name="permissionResolver" ref="permissionResolver"/> <!-- 配置缓存相关 --> <!-- 启用缓存 --> <property name="cachingEnabled" value="true"/> <!-- 开启认证缓存--> <property name="authenticationCachingEnabled" value="true"/> <!-- 指定认证缓存的名字(与 ehcache.xml 中声明的相同) --> <property name="authenticationCacheName" value="shiro-authenticationCache"/> <!--开启授权缓存--> <property name="authorizationCachingEnabled" value="true"/> <!-- 指定授权缓存的名字(与 ehcache.xml 中声明的相同) --> <property name="authorizationCacheName" value="shiro-authorizationCache"/> </bean> <!-- 自定义一个权限匹配器 --> <bean id="permissionResolver" class="com.liwei.shiro.permission.UrlPermissionResolver"/> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- 如果认证不通过,浏览器通过 Get 方式请求到 /login 上 --> <property name="loginUrl" value="/login"/> <!-- override these for application-specific URLs if you like: <property name="successUrl" value="/home.jsp"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> --> <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean --> <!-- defined will be automatically acquired and available via its beanName in chain --> <!-- definitions, but you can perform instance overrides or name aliases here if you like: --> <!-- <property name="filters"> <util:map> <entry key="anAlias" value-ref="someFilter"/> </util:map> </property> --> <property name="filterChainDefinitions"> <value> /admin/**=authc,resourceCheckFilter /login=anon </value> </property> </bean> <!-- 声明一个自定义的过滤器 --> <bean id="resourceCheckFilter" class="com.liwei.shiro.filter.ResourceCheckFilter"> <!-- 为上面声明的自定义过滤器注入属性值 --> <property name="errorUrl" value="/unAuthorization"/> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. --> <!-- 设置安全管理器的安全数据源为自定义的 Realm --> <property name="realm" ref="myRealm"/> <!-- By default the servlet container sessions will be used. Uncomment this line to use shiro's native sessions (see the JavaDoc for more): --> <!-- <property name="sessionMode" value="native"/> --> <property name="cacheManager" ref="ehCacheManager"/> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 配置 shiro 的 ehcache 缓存相关,这个缓存只和 Realm 相关 --> <bean id="ehCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"></bean> <!-- 配置 Spring 的 EhCacheManagerFactoryBean ,须要 spring-context-support 的支持 --> <bean id="ehCacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> <!-- 配置 Spring 的 EhCacheCacheManager,须要 spring-context-support 的支持 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManagerFactoryBean"/> </bean></beans>
步骤3:将原来 web.xml 中关于 Shiro 集成 Web 项目的配置改成 Shiro 集成 Spring 的配置
<!-- 添加 Shiro 相关配置(应该去官网查询相应的配置信息) --><!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml --><filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param></filter><!-- Make sure any request you want accessible to Shiro is filtered. /* catches all --><!-- requests. Usually this filter mapping is defined first (before all others) to --><!-- ensure that Shiro works in subsequent filters in the filter chain: --><filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
步骤4:在加载 Spring 的监听器参数中加入刚刚翻译好的 Shiro 集成 Spring 的配置文件,代码片段如下
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/spring-service.xml, classpath:spring/spring-dao.xml, classpath:spring/spring-shiro.xml </param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
注意:Shiro 集成 Spring 应该添加到父上下文中去,而不能添加到 Spring MVC 的子上下文中。
0 0
- SSM + Shiro 整合 (6)- Shiro 集成 Spring
- SSM + Shiro 整合 (2)- 实现 Spring 集成 MyBatis
- 【shiro】--- spring整合shiro
- SSM + Shiro 整合 (1)- 实现 Spring 的 HelloWorld
- ssm+freemark集成shiro
- ssm+freemark集成shiro
- ssm项目集成shiro
- ssm整合shiro
- ssm+shiro+UEditor整合
- SSM+shiro整合
- 7、ssm整合shiro
- SSM整合shiro完整版
- (三)Shiro集成Spring
- shiro spring整合(一)
- Apache Shiro之实际运用(整合到Spring中)将Shiro整到SSM中(基于maven)
- ssm+freemarker集成shiro修改
- SSM集成安全框架shiro
- shiro与SSM项目整合
- 《Thinkin in JAVA》前言
- httputild请求数据
- 关于漏洞的若干思考
- 北京ICPC题目3 : A Simple Job
- 摄像机模型与标定——单应性
- SSM + Shiro 整合 (6)- Shiro 集成 Spring
- 第26周-window程序设计(基础篇)-第5章(图形基础)-DEVCAPS1.C
- iOS 9 地图定位崩溃
- 源码二维码
- 摄像机模型与标定——摄像机标定
- 购物车
- seleter切换
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- 存储过程截取字符串