权限控制框架Shiro简单介绍及配置实例

Shiro是什么

http://shiro.apache.org/

Apache Shiro是一个非常易用的Java安全框架,它能提供验证、授权、加密和Session控制。Shiro非常轻量级,而且API也非常易于理解,可以使用Shiro完成从APP到企业级应用的所有权限控制。

宏观视图

从宏观来看,Shiro架构中有3个重要概念,Subjct、SecurityManager和Realms。

 

ŸSubject

Subject实际上是正在执行的用户的抽象,“用户”这里可以指自然人,第三方服务,代理账户或者其他。

Subject被绑定在SecurityManager上,当我们与Subject交互时,实际上是与SecurityManager交互。

SecurityManager

SecurityManager是Shiro权限架构的核心,内部维护了一系列安全组件。然而,我们一旦将其配置完成,真正给用户强相关的就是Subject接口了。

当我们操作subject时,实际上就是在操作SecurityManager。

Realms

Reamls是Shiro与我们应用的安全数据沟通的桥梁,在Realm中真正实现用户登录与授权的逻辑。

从这个角度上来讲,Realms其实是一个安全领域的DAO,发将相关数据封装并提供给Shiro,当使用Shiro时,我们必须制定至少一个Realms。

SecurityManager可以配置多个Realms,但是至少一个。

Shiro已经提供了默认的DAO实现,如LDAP和JDBC,另外我们也能实现自己的DAO(例如使用Redis)。

细节视图

 

ŸSubject(org.apache.shiro.subject.Subject)

与当前软件交互的安全视角内的用户抽象(用户、第三方服务)

SecurityManager(org.apache.shiro.mgt.SecurityManager)

Shiro安全框架的核心,像保护伞一样管理着和协调其内部组件,确保其协调运行。它也维护着每个用户的Shiro角色,因此它知道用户的所有安全操作。

Authenticator(org.apache.shiro.authc.Authenticator)

负责执行和验证用户登录行为的组件,当一个用户试图登录,该逻辑是由Authenticator执行的。Authenticator知道如何去协调一个或者更多的realms,这些realms保存着用户信息。而且realms中的数据被取出用来验证用户。

Authentication Strategy(org.apache.shiro.)

如果配置了多个realms,Authentication Strategy将负责协调各Realms的判断逻辑。

ŸAuthorizer(org.apache.shiro.authz.Authorizer)

用户控制用户访问,主要是决定用户能否访问某些资源。类似于Authenticator,Authorizer也知道如何协调多个数据源并据此判断这些用户能否执行某个给定的Action。

SessionManager(org.apache.shiro.session.mgt.SessionManager)

SessionManager知道怎样创建和管理用户Session生命周期,从而为用户提供一个健壮的Session体验。这种机制是Shiro的独创,即使不是Web工程,Shiro也能提供内置的Session机制。

SessionDao负责存取Session。

  • SessionDao(org.apache.shiro.session.mgt.eis.SessionDao)

SessionDao替SessionManager完成Session的CRUD操作,它允许任何Session保存方式(Redis/Memcache/DB..)

CacheManager(org.apache.shiro.cache.CacheManager)

用来保存Shiro使用的鉴权数据,可以使用任何现有的cache产品。

Cryptography(org.apache.shiro.crypto.*)

加密工具包,根据需要使用

Realms(org.apache.shiro.realm.Realm)

真正与安全相关数据(例如账户),我们可以创建任意多的Realm。

配置实例

<?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"

    default-lazy-init="true" xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 

    <description>Shiro Configuration</description>

    <!-- Shiro Filter -->

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

        <!-- 安全管理器 -->

        <property name="securityManager" ref="securityManager" />

        <!-- 登陆的Url,暂时没用 -->

        <property name="loginUrl" value="" />

        <!-- 登录成功的Url,未用 -->

        <property name="successUrl" value="/web/index.html" />

        <!-- 为通过验证的URL -->

        <property name="unauthorizedUrl" value="/index.html" />

        <property name="filters">

            <map>

                <entry key="authc" value-ref="formAuthenticationFilter" />

            </map>

        </property>

        <property name="filterChainDefinitions">

            <ref bean="shiroFilterChainDefinitions" />

        </property>

    </bean>

    <!-- Shiro权限过滤过滤器定义 -->

    <bean name="shiroFilterChainDefinitions" class="java.lang.String">

        <constructor-arg>

            <value>

                <!-- anon表示不校验 -->

                /bower_components/** = anon

                

                /info/home/Vh1/**=anon

                /=anon

                <!-- 剩余请求均经过authc -->

                /** = authc            

            </value>

        </constructor-arg>

    </bean>

    <!-- Form认证过滤器 -->

    <bean id="formAuthenticationFilter"

        class="com.xxxx.xxxx.system.security.FormAuthenticationFilter" />

    <!-- 定义Shiro安全管理配置 -->

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

        <!-- 单realm应用。如果有多个realm,使用‘realms’属性代替 -->

        <!-- 管理认证和授权 -->

        <property name="realm" ref="systemAuthorizingRealm" />

        <!-- 仅shiro适用 -->

        <property name="cacheManager" ref="shiroCacheManager" />

        <!-- 分布式或单机session缓存 -->

        <property name="sessionManager" ref="sessionManager" />

        <!-- 自动登录使用,暂时没有使用 -->

        <property name="rememberMeManager" ref="rememberMeManager" />

    </bean>

 

    <bean id="systemAuthorizingRealm"

        class="com.emcc.xxxx.system.security.shiro.SystemAuthorizingRealm" />

 

    <!-- 自定义会话管理配置 -->

    <bean id="sessionManager"

        class="com.emcc.xxxx.system.security.shiro.session.SessionManager">

        <!-- Redis/本地保存 -->

        <property name="sessionDAO" ref="sessionDAO" />

        <!-- 会话超时时间,配置在system.properties 单位:毫秒 -->

        <property name="globalSessionTimeout" value="${session.sessionTimeout}" />

        <!-- 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话 -->

        <property name="sessionValidationInterval" value="${session.sessionTimeoutClean}" />

        <property name="sessionValidationSchedulerEnabled" value="false" />

        <!-- 配置cookie中sessionid的key -->

        <property name="sessionIdCookie" ref="sessionIdCookie" />

        <property name="sessionIdCookieEnabled" value="true" />

    </bean>

 

    <!-- 指定本系统SESSIONID, 默认为: JSESSIONID 问题: 与SERVLET容器名冲突, 如JETTY, TOMCAT 等默认JSESSIONID,

        当跳出SHIRO SERVLET时如ERROR-PAGE容器会为JSESSIONID重新分配值导致登录会话丢失! -->

    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">

        <constructor-arg name="name" value="web.session.id" />

    </bean>

    <!-- 自定义Session存储容器,集群环境使用 <bean id="sessionDAO" class="com.emcc.xxxx.system.security.shiro.session.JedisSessionDAO">

        <property name="sessionIdGenerator" ref="idGen" /> <property name="sessionKeyPrefix"

        value="${redis.keyPrefix}_session_" /> </bean> -->

    <!-- 自定义Session存储容器,开发环境使用 -->

    <bean id="sessionDAO"

        class="com.emcc.xxxx.system.security.shiro.session.CacheSessionDAO">

        <property name="sessionIdGenerator" ref="idGen" />

        <property name="activeSessionsCacheName" value="activeSessionsCache" />

        <property name="cacheManager" ref="shiroCacheManager" />

    </bean>

 

    <!-- rememberMe管理器 -->

    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">

        <!-- rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位) -->

        <property name="cipherKey"

            value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}" />

        <property name="cookie" ref="rememberMeCookie" />

    </bean>

    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">

        <constructor-arg value="rememberMe" />

        <property name="httpOnly" value="true" />

        <property name="maxAge" value="2592000" /><!-- 30天 -->

    </bean>

    <!-- 定义授权缓存管理器 <bean id="shiroCacheManager" class="com.emcc.xxxx.system.security.shiro.cache.SessionCacheManager"

        /> -->

    <!-- 定义授权缓存管理器 -->

    <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

        <property name="cacheManager" ref="cacheManager" />

    </bean>

 

    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

 

    <!-- securityManager -->

    <bean

        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

        <property name="staticMethod"

            value="org.apache.shiro.SecurityUtils.setSecurityManager" />

        <property name="arguments" ref="securityManager" />

    </bean>

 

    <!-- AOP式方法级权限检查 -->

 

    <!-- AOP式方法级权限检查 这两个类主要用于注解 -->

    <bean

        class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

        <property name="securityManager" ref="securityManager" />

    </bean>

 

</beans>
原文地址:https://www.cnblogs.com/jiyuqi/p/5345731.html