- 浏览: 31921 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
gezi2213:
commons-httpclient-3.0.jar
传个包 ...
httpclient 调用webservice
Apache Shrio是一个安全认证框架,和Spring Security相比,在于他使用了和比较简洁易懂的认证和授权方式。其提供的native-session(即把用户认证后的授权信息保存在其自身提供Session中)机制,这样就可以和HttpSession、EJB Session Bean的基于容器的Session脱耦,到到和客户端应用、Flex应用、远程方法调用等都可以使用它来配置权限认证。
1、sessionMode
在普通的WEB项目中,我们可以选择使用native session或者是HttpSession,通过设置securityManager的sessionMode参数为http或native即可。
2、realm
我们可以基于jdbc,ldap,text,activeDirectory,jndi等多种方式来获取用户基本信息,角色信息,权限信息等。只需要在securityManager中指定使用相应的realm实现即可,其在这各方面都提供了对应的缺省实现,比如我们常用的基于数据库表的形式来配置用户权限信息,就可以使用其缺省实现的jdbcRealm(org.apache.shiro.realm.jdbc.JdbcRealm)。
当然,如果认证信息来自于多方面,多个不同的来源(比如来自两个库中,或者一个数据库,一个是ldap,再配上一个缺省的基于文本的测试用等等),我们可以为securityManager指定realms参数,即把这一组安全配置都配置上。各个具体的realm实现提供了方法来获取用户基本信息、角色、权限等。 realm的授权信息可以存放在Cache中,Cache的名称可以通过设置其authorizationCacheName参数指定。
3、缓存
目前Shrio缺省提供了基于ehCache来缓存用户认证信息和授权信息的实现。只需要配置 org.apache.shiro.web.mgt.DefaultWebSecurityManager 这个 cacheManager并设置给SecurityManager即可。
如果项目中已经存在使用的ehCacheManager配置(org.springframework.cache.ehcache.EhCacheManagerFactoryBean),DefaultWebSecurityManager则可以指定使用现有的ehCacheManager,如果不指定,它将自行使用缺省配置创建一个。
同时,也可以设置cacheManagerConfigFile参数来指定ehCache的配置文件。 下例中的shiro.authorizationCache是用来存放授权信息的Cache,我们在配置realm(如myRealm或jdbcReaml)时,把authorizationCacheName属性设置shiro.authorizationCache来对应。
ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir/tuan-oauth"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- We want eternal="true" (with no timeToIdle or timeToLive settings) because Shiro manages session
expirations explicitly. If we set it to false and then set corresponding timeToIdle and timeToLive properties,
ehcache would evict sessions without Shiro's knowledge, which would cause many problems
(e.g. "My Shiro session timeout is 30 minutes - why isn't a session available after 2 minutes?"
Answer - ehcache expired it due to the timeToIdle property set to 120 seconds.)
diskPersistent=true since we want an enterprise session management feature - ability to use sessions after
even after a JVM restart. -->
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
<cache name="shiro.authorizationCache"
maxElementsInMemory="100"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"/>
</ehcache>
当我们把securityManager的sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCache 的Cache中
org.apache.shiro.web.mgt.DefaultWebSecurityManager 在sessionMode参数设置为native时,缺省使用的是DefaultWebSessionManager来管理Session,该管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。
如果系统内的用户数特别多,我们需要使用CacheSessionDao来基于Cache进行操作,因此,这里需要显示配置一个sessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager),并配置该sessionManager的sessionDao为CacheSessionDao(org.apache.shiro.session.mgt.eis.CachingSessionDAO,需用其实现类org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO)。
配置CacheSessionDao时,我们可以指定属性activeSessionsCacheName的名称来替换掉缺省名 shiro-activeSessionCache。我们再把该sessionManager配置给DefaultWebSecurityManager就可以了。
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<property name="sessionMode" value="native" />
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="realm" ref="myRealm" />
<property name="sessionManager" ref="sessionManager" />
</bean>
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO" />
</bean>
<bean id="sessionDAO"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
</bean>
从以上我们可以看出
a、我们可以指定sessionManager的sessionDao,在某些情况下,我们也可以通过实现自定义的sessionDao来把用户认证信息保存在memcache,mongodb,ldap,database中,达到和其他应用共享用户认证信息的目的,以此达到SSO的目的(当然,sessionId得一致,这个属于我们可以在应用商定怎么设定一致的sessionId的问题)。
b、cacheManager我们也可以自己实现一个,可以根据应用情况来考虑,比如存放在memcache中之类。
4、配置
Web项目中,普通的web项目可以采用ini文件来对shiro进行配置。基于spring的项目可以采用和Spring集成的方式配置。 基于Spring集成的Web项目的基本配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- ========================================================= Shiro Core
Components - Not Spring Specific ========================================================= -->
<!-- Shiro's main business-tier object for web-enabled applications (use
DefaultSecurityManager instead when there is no web environment) -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="sessionMode" value="native" />
<property name="realm" ref="myRealm" />
</bean>
<!-- Let's use some enterprise caching support for better performance. You
can replace this with any enterprise caching framework implementation that
you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- Set a net.sf.ehcache.CacheManager instance here if you already have
one. If not, a new one will be creaed with a default config: -->
<property name="cacheManager" ref="ehCacheManager" />
<!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance
to inject, but you want a specific Ehcache configuration to be used, specify
that here. If you don't, a default will be used.: <property name="cacheManagerConfigFile"
value="classpath:some/path/to/ehcache.xml"/> -->
</bean>
<!-- Used by the SecurityManager to access security data (users, roles,
etc). Many other realm implementations can be used too (PropertiesRealm,
LdapRealm, etc. -->
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="name" value="jdbcRealm" />
<property name="dataSource" ref="dataSource" />
<property name="credentialsMatcher">
<!-- The 'bootstrapDataPopulator' Sha256 hashes the password (using the
username as the salt) then base64 encodes it: -->
<bean class="org.apache.shiro.authc.credential.Sha256CredentialsMatcher">
<!-- true means hex encoded, false means base64 encoded -->
<property name="storedCredentialsHexEncoded" value="false" />
<!-- We salt the password using the username, the most common practice: -->
<property name="hashSalted" value="true" />
</bean>
</property>
<property name="authorizationCacheName" value="shiro.authorizationCache" />
</bean>
<bean id="myRealm" class="org.apache.shiro.realm.text.IniRealm"
init-method="init">
<property name="name" value="myRealm" />
<property name="authorizationCacheName" value="shiro.authorizationCache" />
<property name="resourcePath" value="classpath:config/myRealm.ini" />
</bean>
<!-- ========================================================= Shiro Spring-specific
integration ========================================================= -->
<!-- Post processor that automatically invokes init() and destroy() methods
for Spring-configured Shiro objects so you don't have to 1) specify an init-method
and destroy-method attributes for every bean definition and 2) even know
which Shiro objects require these methods to be called. -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after
the lifecycleBeanProcessor has run: -->
<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>
<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations
can be associated with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor"
class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- Define the Shiro Filter here (as a FactoryBean) instead of directly
in web.xml - web.xml uses the DelegatingFilterProxy to access this bean.
This allows us to wire things with more control as well utilize nice Spring
things such as PropertiesPlaceholderConfigurer and abstract beans or anything
else we might need: -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/index" />
<property name="unauthorizedUrl" value="/unauthorized" />
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter
bean defined will be automatically acquired and available via its beanName
in chain definitions, but you can perform overrides or parent/child consolidated
configuration here if you like: -->
<!-- <property name="filters"> <util:map> <entry key="aName" value-ref="someFilterPojo"/>
</util:map> </property> -->
<property name="filterChainDefinitions">
<value>
/login = authc
/account = user
/manage = user,roles[admin]
</value>
</property>
</bean>
</beans>
5、基于url资源的权限管理
我们可以简单配置在shiroFilter的filterChainDefinitions中,也可以考虑通过一个文本文件,我们读入内容后设置进去。或者通过Ini类来装入Ini文件内容,到时取出urls的部分来设置给shiroFilter的filterChainDefinitions。也可以把这部分数据存入数据库表中,到时读出一个Map来设置给shiroFilter的filterChainDefinitionsMap属性。
6、url的配置
authc是认证用户(rememberMe的用户也必须再次登录才能访问该url),配置成user才能让rememberMe用户也可以访问。
7、rememberMe Cookie的处理
Shiro有一套缺省机制,由CookieRememberMeManager实现。其有一个SimpleCookie类,保存对应的用户信息等。
每次保存时,系统把SimpleCookie的信息设置好之后,先用DefaultSerializer把其用jvm缺省序列化方式序列化成byte[],然后再用cipherService(缺省是aes加密算法)来加密该byte[],最后用Base64.encodeToString(serialized)压缩成一个字符串,再写入名称为rememberMe的Cookie中。
读取时,通过把该rememberMe Cookie的内容用byte[] decoded = Base64.decode(base64)解压出该byte[],再用cipherService解密,最后用DefaultSerializer反序列化出来该SimpleCookie类。
如果我们有自定义的rememberMe Cookie需要处理,特别是在和其他网站一起SSO,通过访问主域的Cookie来获取记录的用户信息时,我们需要重新实现rememberMeManager(可以考虑继承AbstractRememberMeManager),和根据实际用的序列化方式Serializer来实现一个(比如考虑通用性,用json方式序列化)。
在Spring配置中,配置好RememberMeManager,装配上sericerlizer和cipherService(根据实际情况选用适当的加密算法),最后把rememberMeManager设置给DefaultWebSecurityManager即可。如果非常简单的cookie,可以直接实现RememberMeManager的几个接口方法也行。
Link:http://www.blogjava.net/paulwong/archive/2012/01/28/368930.html
1、sessionMode
在普通的WEB项目中,我们可以选择使用native session或者是HttpSession,通过设置securityManager的sessionMode参数为http或native即可。
2、realm
我们可以基于jdbc,ldap,text,activeDirectory,jndi等多种方式来获取用户基本信息,角色信息,权限信息等。只需要在securityManager中指定使用相应的realm实现即可,其在这各方面都提供了对应的缺省实现,比如我们常用的基于数据库表的形式来配置用户权限信息,就可以使用其缺省实现的jdbcRealm(org.apache.shiro.realm.jdbc.JdbcRealm)。
当然,如果认证信息来自于多方面,多个不同的来源(比如来自两个库中,或者一个数据库,一个是ldap,再配上一个缺省的基于文本的测试用等等),我们可以为securityManager指定realms参数,即把这一组安全配置都配置上。各个具体的realm实现提供了方法来获取用户基本信息、角色、权限等。 realm的授权信息可以存放在Cache中,Cache的名称可以通过设置其authorizationCacheName参数指定。
3、缓存
目前Shrio缺省提供了基于ehCache来缓存用户认证信息和授权信息的实现。只需要配置 org.apache.shiro.web.mgt.DefaultWebSecurityManager 这个 cacheManager并设置给SecurityManager即可。
如果项目中已经存在使用的ehCacheManager配置(org.springframework.cache.ehcache.EhCacheManagerFactoryBean),DefaultWebSecurityManager则可以指定使用现有的ehCacheManager,如果不指定,它将自行使用缺省配置创建一个。
同时,也可以设置cacheManagerConfigFile参数来指定ehCache的配置文件。 下例中的shiro.authorizationCache是用来存放授权信息的Cache,我们在配置realm(如myRealm或jdbcReaml)时,把authorizationCacheName属性设置shiro.authorizationCache来对应。
ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir/tuan-oauth"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- We want eternal="true" (with no timeToIdle or timeToLive settings) because Shiro manages session
expirations explicitly. If we set it to false and then set corresponding timeToIdle and timeToLive properties,
ehcache would evict sessions without Shiro's knowledge, which would cause many problems
(e.g. "My Shiro session timeout is 30 minutes - why isn't a session available after 2 minutes?"
Answer - ehcache expired it due to the timeToIdle property set to 120 seconds.)
diskPersistent=true since we want an enterprise session management feature - ability to use sessions after
even after a JVM restart. -->
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
<cache name="shiro.authorizationCache"
maxElementsInMemory="100"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"/>
</ehcache>
当我们把securityManager的sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCache 的Cache中
org.apache.shiro.web.mgt.DefaultWebSecurityManager 在sessionMode参数设置为native时,缺省使用的是DefaultWebSessionManager来管理Session,该管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。
如果系统内的用户数特别多,我们需要使用CacheSessionDao来基于Cache进行操作,因此,这里需要显示配置一个sessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager),并配置该sessionManager的sessionDao为CacheSessionDao(org.apache.shiro.session.mgt.eis.CachingSessionDAO,需用其实现类org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO)。
配置CacheSessionDao时,我们可以指定属性activeSessionsCacheName的名称来替换掉缺省名 shiro-activeSessionCache。我们再把该sessionManager配置给DefaultWebSecurityManager就可以了。
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<property name="sessionMode" value="native" />
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="realm" ref="myRealm" />
<property name="sessionManager" ref="sessionManager" />
</bean>
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO" />
</bean>
<bean id="sessionDAO"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
</bean>
从以上我们可以看出
a、我们可以指定sessionManager的sessionDao,在某些情况下,我们也可以通过实现自定义的sessionDao来把用户认证信息保存在memcache,mongodb,ldap,database中,达到和其他应用共享用户认证信息的目的,以此达到SSO的目的(当然,sessionId得一致,这个属于我们可以在应用商定怎么设定一致的sessionId的问题)。
b、cacheManager我们也可以自己实现一个,可以根据应用情况来考虑,比如存放在memcache中之类。
4、配置
Web项目中,普通的web项目可以采用ini文件来对shiro进行配置。基于spring的项目可以采用和Spring集成的方式配置。 基于Spring集成的Web项目的基本配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- ========================================================= Shiro Core
Components - Not Spring Specific ========================================================= -->
<!-- Shiro's main business-tier object for web-enabled applications (use
DefaultSecurityManager instead when there is no web environment) -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="sessionMode" value="native" />
<property name="realm" ref="myRealm" />
</bean>
<!-- Let's use some enterprise caching support for better performance. You
can replace this with any enterprise caching framework implementation that
you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- Set a net.sf.ehcache.CacheManager instance here if you already have
one. If not, a new one will be creaed with a default config: -->
<property name="cacheManager" ref="ehCacheManager" />
<!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance
to inject, but you want a specific Ehcache configuration to be used, specify
that here. If you don't, a default will be used.: <property name="cacheManagerConfigFile"
value="classpath:some/path/to/ehcache.xml"/> -->
</bean>
<!-- Used by the SecurityManager to access security data (users, roles,
etc). Many other realm implementations can be used too (PropertiesRealm,
LdapRealm, etc. -->
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="name" value="jdbcRealm" />
<property name="dataSource" ref="dataSource" />
<property name="credentialsMatcher">
<!-- The 'bootstrapDataPopulator' Sha256 hashes the password (using the
username as the salt) then base64 encodes it: -->
<bean class="org.apache.shiro.authc.credential.Sha256CredentialsMatcher">
<!-- true means hex encoded, false means base64 encoded -->
<property name="storedCredentialsHexEncoded" value="false" />
<!-- We salt the password using the username, the most common practice: -->
<property name="hashSalted" value="true" />
</bean>
</property>
<property name="authorizationCacheName" value="shiro.authorizationCache" />
</bean>
<bean id="myRealm" class="org.apache.shiro.realm.text.IniRealm"
init-method="init">
<property name="name" value="myRealm" />
<property name="authorizationCacheName" value="shiro.authorizationCache" />
<property name="resourcePath" value="classpath:config/myRealm.ini" />
</bean>
<!-- ========================================================= Shiro Spring-specific
integration ========================================================= -->
<!-- Post processor that automatically invokes init() and destroy() methods
for Spring-configured Shiro objects so you don't have to 1) specify an init-method
and destroy-method attributes for every bean definition and 2) even know
which Shiro objects require these methods to be called. -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after
the lifecycleBeanProcessor has run: -->
<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>
<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations
can be associated with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor"
class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- Define the Shiro Filter here (as a FactoryBean) instead of directly
in web.xml - web.xml uses the DelegatingFilterProxy to access this bean.
This allows us to wire things with more control as well utilize nice Spring
things such as PropertiesPlaceholderConfigurer and abstract beans or anything
else we might need: -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/index" />
<property name="unauthorizedUrl" value="/unauthorized" />
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter
bean defined will be automatically acquired and available via its beanName
in chain definitions, but you can perform overrides or parent/child consolidated
configuration here if you like: -->
<!-- <property name="filters"> <util:map> <entry key="aName" value-ref="someFilterPojo"/>
</util:map> </property> -->
<property name="filterChainDefinitions">
<value>
/login = authc
/account = user
/manage = user,roles[admin]
</value>
</property>
</bean>
</beans>
5、基于url资源的权限管理
我们可以简单配置在shiroFilter的filterChainDefinitions中,也可以考虑通过一个文本文件,我们读入内容后设置进去。或者通过Ini类来装入Ini文件内容,到时取出urls的部分来设置给shiroFilter的filterChainDefinitions。也可以把这部分数据存入数据库表中,到时读出一个Map来设置给shiroFilter的filterChainDefinitionsMap属性。
6、url的配置
authc是认证用户(rememberMe的用户也必须再次登录才能访问该url),配置成user才能让rememberMe用户也可以访问。
7、rememberMe Cookie的处理
Shiro有一套缺省机制,由CookieRememberMeManager实现。其有一个SimpleCookie类,保存对应的用户信息等。
每次保存时,系统把SimpleCookie的信息设置好之后,先用DefaultSerializer把其用jvm缺省序列化方式序列化成byte[],然后再用cipherService(缺省是aes加密算法)来加密该byte[],最后用Base64.encodeToString(serialized)压缩成一个字符串,再写入名称为rememberMe的Cookie中。
读取时,通过把该rememberMe Cookie的内容用byte[] decoded = Base64.decode(base64)解压出该byte[],再用cipherService解密,最后用DefaultSerializer反序列化出来该SimpleCookie类。
如果我们有自定义的rememberMe Cookie需要处理,特别是在和其他网站一起SSO,通过访问主域的Cookie来获取记录的用户信息时,我们需要重新实现rememberMeManager(可以考虑继承AbstractRememberMeManager),和根据实际用的序列化方式Serializer来实现一个(比如考虑通用性,用json方式序列化)。
在Spring配置中,配置好RememberMeManager,装配上sericerlizer和cipherService(根据实际情况选用适当的加密算法),最后把rememberMeManager设置给DefaultWebSecurityManager即可。如果非常简单的cookie,可以直接实现RememberMeManager的几个接口方法也行。
Link:http://www.blogjava.net/paulwong/archive/2012/01/28/368930.html
发表评论
-
CAS
2012-11-29 15:47 1154单点登录(Single Sign On , ... -
jvm的内存溢出异常
2012-09-25 09:17 771LINK:http://www.cnblogs.com/eva ... -
jBPM4开发入门
2012-08-20 15:23 904jBPM(java Business Process Ma ... -
给js文件传参数
2012-07-10 10:59 2641给js文件传参数 一、利用全局变量 这是最简单的一种方式,比 ... -
Groovy入门教程
2012-06-19 13:39 917Groovy入门教程 kmyhy@126.com 2009- ... -
正则表达式
2012-05-25 14:55 8791、 常用的元字符 . 匹配除换行符以外的任意字符 ... -
tomcat ssh
2012-05-18 16:22 975生成服务器证书: keytool -genkey -v -al ... -
git 多人操作
2012-05-18 15:34 998GIT的介绍就不多说了。这里只是简单的写些入门的操作。 这里 ... -
jetty快速入门与嵌入使用 jetty
2012-05-17 16:09 999看到开源项目发布的时候都带一个 jsp 容器(jetty)。拿 ... -
用Resteasy实现restful web service
2012-05-16 16:26 2340"欢迎您光临敝博, ... -
一些链接
2012-05-08 14:26 652windows 安装 apache php wordpress ...
相关推荐
适用于spring集成的javaweb环境
配置applicationContext-shiro.xml 1. 配置authorizingRealm <bean id="authorizingRealm" class="com.mjm.core.interceptor.ShiroRealm"> 2.Shiro Filter 设置拦截的内容和登录页面和成功、失败页面 3.配置...
1,shiro+hibernate4+spring3+easyui+fusioncharts部分bootstrap样式全注解零配置简单权限管理web项目 2,简单shiro权限管控 细度到按钮 3,安装方法按config.properties参数配置你的数据库 4,运行db.sql 5,把项目导入...
SpringMVC+Mybatis+Spring+Shiro+ehcache整合配置文件
很好的一套spring+mvc+shiro的demo,很容易上手。
项目简介:此项目只是简单的集成spring+springmvc+shiro+ehcahce 二: 步骤说明: 1:项目集成spring 在web.xml中配置spring容器的监听器。 2:项目集成springmvc 在web.xml中配置前端控制器 3:项目集成...
shiro集成spring开发,可以学习怎么配置shiro和spring集成等等
shiro与spring整合 包括jdbcrealm.java及spring配置文件 欢迎联系:jeson_jin@126.com
spring集成shiro 和mongodb数据库 ,下载可用ini配置,启用mongo配置
从web.xml到spring 配置shiro的使用
缓存可以提高查询数据性能, 对同一批数据进行多次查询时, 第一次查询走数据库, 查询数据后,将数据保存在内存中,第二次以后查询 可以直接从内存获取数据,而不需要 和数据库进行交互。
springmvc shiro基本配置,亲测无sql,springmvc shiro基本配置,亲测无sql
Spring Boot 2.0.4 & Shiro1.4.0 权限管理系统。 文章地址:https://xttblog.blog.csdn.net/article/details/88353878 FEBS是一个简单高效的后台权限管理系统。项目基础框架采用全新的Java Web开发框架 —— Spring ...
spring整合shirospring-data-redis和spring-session-data-redis通过shiro实现单点登录
spring、springmvc、shiro集成空框架,包括jar包、配置文件。
本demo为Spring boot整合shiro,以mybatis plus做dao层交互数据,实现了读取数据库用户数据实现用户登录,权限认证,读取数据库中用户对应的url请求,实现请求的过滤。自定义了relam和过滤器来实现这些功能
springsecurity,shiro部分配置
shiro+spring mvc结合的实例源码,包含类库,简单易懂,配置齐全
完整的spring +springmvc+hibernate+shiro项目实例,详细的shiro配置介绍,通过redis管理用户session缓存。。。。。。。。
简介该项目主要利用Spring Boot的自动化配置特性来实现快速的将Shiro集成到SpringBoot应用中源码地址Github:码云:我的博客:自制的小工具,欢迎使用和Star,如果使用过程中遇到问题,可以提出Issue,我会尽力完善...