shiro的session和servlet的session的區別
阿新 • • 發佈:2019-02-09
shiro作為輕量級的許可權管理框架,應用廣泛。在使用過程中需要注意org.apache.shiro.web.session.mgt.DefaultWebSessionManager預設session是shiro原生(native)session,不同於servlet自身的session,在jsp頁面獲取不到session中的值。
一、shiro中session和servlet自身session區別
shiro預設session管理器是DefaultWebSessionManager ,DefaultWebSessionManager 實現自WebSessionManager,我們來看下WebSessionManager的的定義,只有一個方法isServletContainerSessions:
翻譯成中文主要意思就是:如果返回true,使用servlet容器的session,如果返回false,使用shiro自身的session(即native的)。使用shiro的session支援企業級的特性,例如分散式快取。/** * Returns {@code true} if session management and storage is managed by the underlying Servlet container or * {@code false} if managed by Shiro directly (called 'native' sessions). * <p/> * If sessions are enabled, Shiro can make use of Sessions to retain security information from * request to request. This method indicates whether Shiro would use the Servlet container sessions to fulfill its * needs, or if it would use its own native session management instead (which can support enterprise features * - like distributed caching - in a container-independent manner). * * @return {@code true} if session management and storage is managed by the underlying Servlet container or * {@code false} if managed by Shiro directly (called 'native' sessions). */ boolean isServletContainerSessions();
我們再來看一下DefaultWebSessionManager 對isServletContainerSessions方法的實現:
返回false,使用的是shiro自身的session。/** * This is a native session manager implementation, so this method returns {@code false} always. * * @return {@code false} always * @since 1.2 */ public boolean isServletContainerSessions() { return false; }
二、shiro中使用servlet的session
如果我們想要使用servlet的session,需要做的就是(1)寫一個類繼承DefaultWebSessionManager ,複寫isServletContainerSessions,返回true;(2)在shiro配置檔案中使用自定義類。
1.自定義類MyWebSessionManager,繼承DefaultWebSessionManager ,複寫isServletContainerSessions方法
public class MyWebSessionManager extends DefaultWebSessionManager {
@Override
public boolean isServletContainerSessions() {
return true;
}
}
2.在shiro.xml配置檔案使用自定義類
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<description>spring-shiro</description>
<bean id="defaultWebSessionManager"
class="com.bec.risk.controller.security.MyWebSessionManager">
<!-- 3600000 = 1hour -->
<property name="globalSessionTimeout" value="3600000" />
<property name="sessionDAO" ref="sessionDAO" />
<property name="sessionIdUrlRewritingEnabled" value="false" />
<property name="sessionIdCookie.name" value="jsid" />
</bean>
<bean id="sessionDAO"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
<property name="cacheManager" ref="cacheManager" />
</bean>
<!-- 憑證匹配器 -->
<bean id="credentialsMatcher"
class="com.bec.risk.controller.security.RetryLimitHashedCredentialsMatcher">
<constructor-arg ref="cacheManager" />
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="3" />
<property name="storedCredentialsHexEncoded" value="true" />
</bean>
<!-- 自定義的Realm資料來源 -->
<bean id="shiroDbRealm" class="com.bec.risk.controller.security.ShiroDbRealm">
<property name="cachingEnabled" value="true" />
<property name="authenticationCachingEnabled" value="true" />
<property name="authenticationCacheName" value="shiroAuthenticationCache" />
<property name="authorizationCachingEnabled" value="true" />
<property name="authorizationCacheName" value="shiroAuthorizationCache" />
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
<!-- Shiro's main business-tier object for web-enabled applications -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm" />
<property name="cacheManager" ref="cacheManager" />
<property name="sessionManager" ref="defaultWebSessionManager" />
</bean>
<bean id="casFilter"
class="com.bec.risk.controller.security.MyFormAuthenticationFilter" />
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/view/index" />
<property name="unauthorizedUrl" value="/403" />
<property name="filters">
<map>
<entry key="authc" value-ref="casFilter" />
</map>
</property>
<!-- shiro連線約束配置 -->
<property name="filterChainDefinitions">
<!-- anon 匿名過濾器 authc 如果繼續操作,需要做對應的表單驗證否則不能通過 authcBasic 基本http驗證過濾,如果不通過,跳轉到登入頁面
logout 登入退出過濾器 noSessionCreation 沒有session建立過濾器 perms 許可權過濾器 port 埠過濾器,可以設定是否是指定埠如果不是跳轉到登入頁面
rest http方法過濾器,可以指定如post不能進行訪問等 roles 角色過濾器,判斷當前使用者是否指定角色 ssl 請求需要通過ssl,如果不是跳轉回登入頁
user 如果訪問一個已知使用者,比如記住我功能,走這個過濾器 -->
<value>
/swagger-ui.html=authc
/kaptcha = anon
/login=authc
/logout=logout
/assets/** = anon
/static/** = anon
/interface/** = anon
/**=authc
</value>
</property>
</bean>
<!-- 快取管理器 使用Ehcache實現 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
</bean>
<!-- Shiro生命週期處理器 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- 開啟Shiro的註解(如@RequiresRoles,@RequiresPermissions),需藉助SpringAOP掃描使用Shiro註解的類,並在必要時進行安全邏輯驗證 -->
<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass"
value="true" /> </bean> -->
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
</beans>
使用到了ehcache快取,雖然和本主題無關,為方便大家測試,將ehcache.xml配置也貼在下方
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="risk-ehcache">
<diskStore path="java.io.tmpdir" />
<!-- DefaultCache setting. -->
<defaultCache maxEntriesLocalHeap="10000" eternal="true"
timeToIdleSeconds="12000" timeToLiveSeconds="12000"
overflowToDisk="false" diskPersistent="false" maxEntriesLocalDisk="100000" />
<cache name="passwordRetryCache" maxElementsInMemory="20000"
maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="12000" timeToLiveSeconds="12000"
overflowToDisk="false" diskPersistent="false"
memoryStoreEvictionPolicy="LRU" statistics="true">
</cache>
<cache name="shiro-activeSessionCache"
maxElementsInMemory="30000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="12000"
timeToLiveSeconds="0"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
<cache name="shiroAuthenticationCache"
maxElementsInMemory="30000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
<cache name="shiroAuthorizationCache"
maxElementsInMemory="30000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="12000"
timeToLiveSeconds="0"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>