Shiro集成SpringMVC

一个非凡的网站 http://www.hikson.com,RJ海客森

视频录制

  1. git地址:https://gitee.com/hikseason/demo-spring-boot-all.git
  2. 视频地址:https://pan.baidu.com/s/19mrxCE9Y5R5ntSEg_EReOg

1.Shiro集成SpringMVC

  1. 就是定了了个过滤器shiroFilter,然后注入securityManager管理权限
  2. securityManager又注入了MyRealm,可以你自己实现权限管理

2.web.xml的配置

1.实例化Spring容器

  1. 实例化Spring容器,并制定classpath:spring.xml
<!-- 指定Spring的配置文件 -->  
<!-- 否则Spring会默认从WEB-INF下寻找配置文件,contextConfigLocation属性是Spring内部固定的 -->  
<!-- 通过,ContextLoaderListener,的父类ContextLoader的第120行发现CONFIG_LOCATION_PARAM固定为contextConfigLocation -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring.xml
    </param-value>
</context-param>

<!-- 实例化Spring容器 -->  
<!-- 应用启动时,该监听器被执行,它会读取Spring相关配置文件,其默认会到WEB-INF中查找applicationContext.xml -->  
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2.配置shiro过滤器,优先处理的

  1. 注意这个shiroFilter,在内部是确实存在这个bean的,在spring-shiro.xml中配置了
  2. targetFilterLifecycle默认为false,这里改为true,表示生命周期交给ServletContainer管理
<!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 -->  
<!-- 这里filter-name必须对应spring-shiro.xml中定义的<bean id="shiroFilter"/> -->  
<!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->  
<!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 -->  
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->  
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.web.xml中配置了shiroFilter,注意名字

注意需要在spring-shiro.xml中一致起来

3.spring-shiro.xml解读

  1. 这里的相关定义可以从shiro-realm.ini,进行解读
[main]
myRealm1=org.rainbow.shiro.realm.MyRealm

#===================授权缓存===============
myRealm1.authorizationCachingEnabled=true
myRealm1.authorizationCacheName=authorizationCache

#==================验证缓存=======
myRealm1.authenticationCachingEnabled=true
myRealm1.authenticationCacheName=authenticationCache

#==================设置缓存的实现以及配置=======
cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml
securityManager.cacheManager=$cacheManager
#==================================

securityManager.realms=$myRealm1

[users]
rainbow=123456

1.定义了过滤器shiroFilter

  1. 这个过滤器需要参数securityManager
  2. shiroFilter过滤器中的定义就可以走上述shiro-realm.ini了
  3. 定义了url相关

2.SecurityManager需要realm和cacheManager

  1. securityManager需要参数realm,
  2. 还需要参数securityManager.cacheManager=$cacheManager
<!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->  
<!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->  
<!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 --> 
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  <property name="cacheManager" ref="cacheManager"/>   
  <property name="realm" ref="myRealm"/> 
</bean>  

3.定一个realm,myRealm

  1. 看上面的===================授权缓存===============
  2. 对myRealm定义属性:authorizationCachingEnabled为true
  3. 定义入参:authenticationCacheName为authorizationCache
<!-- 继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录的类为自定义的MyRealm.java --> 
<bean id="myRealm" class="org.rainbow.shiro.realm.MyRealm">
    <property name="authorizationCachingEnabled" value="true"/> 
    <property name="authorizationCacheName" value="authorizationCache"/>
</bean>

4.定义一个cacheManager缓存管理器

<!-- 配置缓存管理器 -->  
 <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
 <!-- 指定 ehcache 的配置文件 -->   
      <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>   
 </bean>

5.指定配置文件shiro-ehcache.xml

4.注解与页面权限

1.开启注解功能RequiresRoles和RequiresPermissions

  1. 注意要开启spring的aop功能
<!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证 -->  
<!-- 配置以下两个bean即可实现此功能 -->  
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run -->  
<!-- 由于本例中并未使用Shiro注解,故注释掉这两个bean(个人觉得将权限通过注解的方式硬编码在程序中,查看起来不是很方便,没必要使用) -->  
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>

2.注解使用

@RequestMapping("/")
@RequiresRoles("amdin")
@RequiresPermissions("user:create")
public String index(){
    return "index";
}

5.测试

  1. 访问登录:http://localhost:8080/
  2. 访问角色:http://localhost:8080/adminRole
  3. 访问权限:http://localhost:8080/userCreate
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值