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.)
如果配置了多个realmsAuthentication Strategy将负责协调各Realms的判断逻辑。
Authorizer(org.apache.shiro.authz.Authorizer)
用户控制用户访问主要是决定用户能否访问某些资源。类似于AuthenticatorAuthorizer也知道如何协调多个数据源并据此判断这些用户能否执行某个给定的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/Vh2/**=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>
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。