首先
多Realm认证,首先得有多个realm对吧,先搞两个,之前已经在spring中配置了一个realm了,再添加一个新的Realm。
- 这个新的Realm改一下它的加密参数就行了,其它的不变
<!--实现了Realm接口的实现类,一开始就配置好的那个-->
<bean id="jdbcRealm" class="com.xxx.shiro.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"/>
<property name="hashIterations" value="1024"/>
</bean>
</property>
</bean>
<!--实现了Realm接口的实现类,新加的用SHA1的方式加密-->
<bean id="secondRealm" class="com.xxx.shiro.ShrioSecondRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="SHA1"/>
<property name="hashIterations" value="1024"/>
</bean>
</property>
</bean>
- 还要在securityManager中引入这个新加的Realm,之前用的是Realm属性,现在添加两个就不能用之前配置的realm属性了,要用realms属性(Σ( ° △ °|||)︴是不是很好理解……)
<!--配置securityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="secondRealm"/>
</list>
</property>
</bean>
其次
有了多个Realm,现在是按照配置的顺序认证的,自然而然就有怎么认证才算数的问题,这时候就需要认证策略来决定到底怎么办o(∩_∩)o
认证策略是AuthenticationStrategy接口的默认实现
- FirstSuccessfulStrategy:只要一个成功即可,只返回第一个成功的认证信息。
- AtLeastOneSuccessfulStrategy:只要一个成功即可,返回所有成功的认证信息
- AllSuccessfulStrategy:所有的都成功才行,返回所有成功的认证信息
默认的应该是至少一个的那个。还有,所谓的认证信息就是Realm中最后要返回的那个simple什么的对象的构造函数里面传的第一个参数。还有,失败的信息会被忽略。
要配置认证策略,要再配置一个如下的类,在该类的属性中配置相关的认证策略。最后把这个类配置到securityManager的authenticator属性当中即可。
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<!--配置认证策略为全部成功才可以-->
<property name="authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/>
</property>
</bean>
securityManager的新配置方法
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="authenticator" ref="authenticator"/>
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="secondRealm"/>
</list>
</property>
</bean>