Shiro系统权限管理、及原理剖析

本文介绍了Apache Shiro的安全框架,包括其在JavaEE中的广泛应用,相较于Spring Security的轻量级特性,以及它提供的用户身份验证、密码加密、会话管理等功能。深入解析了Shiro的原理,并展示了详细的配置过程,包括Realm实现、会话管理器、安全管理器等配置,以及如何使用Shiro进行权限控制和会话管理。此外,还提及了Shiro与Redis的整合,用于缓存管理和会话持久化。
摘要由CSDN通过智能技术生成

1.简介       


        常用的JavaEE安全框架有shiro、spring security。shiro被应用非常广泛,可以集成cas,搭建单点登录系统。spring security则被认为比较重,应用没有shiro广泛。shiro提供用户名、密码验证,及密码的加密存储,会话Session的管理,与web集成,支持HTTPS的拦截。


2.Shiro原理简析


shiro 原理剖析:

       shiro的核心是java servlet规范中的filter,通过配置拦截器,使用拦截器链来拦截请求,如果允许访问,则通过。通常情况下,系统的登录、退出会配置拦截器。登录的时候,调用subject.login(token),token是用户验证信息,这个时候会在Realm中doGetAuthenticationInfo方法中进行认证。这个时候会把用户提交的验证信息与数据库中存储的认证信息进行比较,一致则允许访问,并在浏览器种下此次回话的cookie,在服务器端存储session信息。退出的时候,调用subject.logout(),会清除回话信息。

      shiro中核心概念介绍:
         Filter:
                 1.AnonymousFilter:通过此filter修饰的url,任何人都可以进行访问,即使没有进行权限认证
                 2.FormAuthenticationFilter:通过此filter修饰的url,会对请求的url进行验证,如果没有通过,则会重定向返回到loginurl
                 3.BasicHttpAuthenticationFilter:通过此filter修饰的url,要求用户已经通过认证,如果没有通过,则会要求通过Authorization 信息进行认证
                 4.LogoutFilter:通过此filter修饰的url,一旦收到url请求,则会立即调用subject进行退出,并重定向到redirectUrl
                 5.NoSessionCreationFilter:通过此filter修饰的url,不会创建任何会话
                 6.PermissionAuthorizationFilter:权限拦截器,验证用户是否具有相关权限
                 7.PortFilter:端口拦截器,不是通过制定端口访问url,将自动将端口重定向到指定端口
                 8.HttpMethodPermissionFilter:rest风格拦截器,配置rest的访问方式
                 9.RolesAuthorizationFilter:角色拦截器,未登陆,将跳转到loginurl,未授权,将跳转到unauthorizedUrl
                 10.SslFilter:HTTPS拦截器,需要以HTTPS的方式进行访问
                 11.UserFilter:用户拦截器,需要用户已经认证,或已经remember me


         Subject:
                 表示当前操作的主体用户,是一个抽象的概念,Subject可以进行登录、退出、权限判断等动作,通常一个subject与一个线程进行关联。


         Realm:
                表示验证的数据源,存储用户的安全数据,可以进行用户名和密码的匹配,及用户权限查询。


3.Shiro配置方法


shiro 配置方法:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:util="http://www.springframework.org/schema/util"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd  
  7.             http://www.springframework.org/schema/aop  
  8.             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  9.             http://www.springframework.org/schema/context  
  10.             http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  11.             http://www.springframework.org/schema/tx  
  12.             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  13.   
  14.     <description>Apache Shiro Security Configuration</description>  
  15.   
  16.     <!-- Realm实现 -->  
  17.     <bean id="userRealm" class="com.ttyc.mammon.service.shiro.UserRealm">  
  18.         <property name="cachingEnabled" value="false"/>  
  19.     </bean>  
  20.   
  21.     <!-- 会话ID生成器 -->  
  22.     <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>  
  23.   
  24.     <!-- 会话Cookie模板 -->  
  25.     <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">  
  26.         <constructor-arg value="sid"/>  
  27.         <property name="domain" value=".ttyongche.com"/>  
  28.         <property name="path" value="/"/>  
  29.         <property name="httpOnly" value="false"/>  
  30.         <property name="maxAge" value="-1"/>  
  31.     </bean>  
  32.   
  33.     <!-- 会话DAO -->  
  34.     <bean id="sessionDAO" class="org.crazycake.shiro.RedisSessionDAO">  
  35.         <property name="sessionIdGenerator" ref="sessionIdGenerator"/>  
  36.         <property name="redisManager" ref="redisManager"/>  
  37.     </bean>  
  38.   
  39.     <!-- 会话管理器 -->  
  40.     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">  
  41.         <property name="globalSessionTimeout" value="1800000"/>  
  42.         <property name="deleteInvalidSessions" value="true"/>  
  43.         <property name="sessionDAO" ref="sessionDAO"/>  
  44.         <property name="sessionIdCookieEnabled" value="true"/>  
  45.         <property name="sessionIdCookie" ref="sessionIdCookie"/>  
  46.     </bean>  
  47.   
  48.     <!-- 安全管理器 -->  
  49.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  50.         <property name="realm" ref="userRealm"/>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值