ApacheShiro:如何使用SpringApplicationContext设置身份验证策略?
在基于spring的web应用程序中,我一直在努力使用shiro 1.2.1进行authenticationStrategy设置。我有两个领域。一个针对ApacheShiro:如何使用SpringApplicationContext设置身份验证策略?,spring,jakarta-ee,web,shiro,realm,Spring,Jakarta Ee,Web,Shiro,Realm,在基于spring的web应用程序中,我一直在努力使用shiro 1.2.1进行authenticationStrategy设置。我有两个领域。一个针对数据库进行身份验证,另一个针对ldap进行身份验证。这两个领域都运行良好,只是我想要一个FirstSuccessfulStrategy,但似乎这两个领域仍在被调用。以下是我的安全应用程序上下文: <bean id="passwordService" class="org.apache.shiro.authc.credential.Defau
数据库进行身份验证,另一个针对ldap进行身份验证。这两个领域
都运行良好,只是我想要一个FirstSuccessfulStrategy
,但似乎这两个领域仍在被调用。以下是我的安全应用程序上下文:
<bean id="passwordService" class="org.apache.shiro.authc.credential.DefaultPasswordService">
<property name="hashService" ref="hashService" />
</bean>
<bean id="hashService" class="org.apache.shiro.crypto.hash.DefaultHashService">
<property name="hashAlgorithmName" value="SHA-512" />
<property name="hashIterations" value="500000" />
</bean>
<bean id="SaltedSha512JPARealm" class="bla.bla.webapp.security.SaltedSha512JPARealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.PasswordMatcher">
<property name="passwordService" ref="passwordService"/>
</bean>
</property>
</bean>
<bean id="ldapContextFactory" class="org.apache.shiro.realm.ldap.JndiLdapContextFactory">
<property name="url" value="${user.ldap.connection.url}"/>
<property name="authenticationMechanism" value="${user.ldap.connection.auth_mecanism}"/>
</bean>
<bean id="ldapRealm" class="bla.bla.webapp.security.LDAPRealm">
<property name="userDnTemplate" value="${user.ldap.connection.userDnTemplate}"/>
<property name="contextFactory" ref="ldapContextFactory" />
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="roleRepository,roleRightRepository,rightRepository,userRepository">
<property name="realms">
<list>
<ref local="ldapRealm"/>
<ref local="SaltedSha512JPARealm"/>
</list>
</property>
<property name="authenticator.authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>
</property>
</bean>
有什么我做得不好的吗?FirstSuccessfulStrategy
意味着您的验证者将尝试所有领域对用户进行身份验证,直到第一次成功。您的领域按顺序配置:ldapRealm
,SaltedSha512JPARealm
。所以,如果lapRealm
将失败,验证器将尝试第二个。要解决此问题,您可以尝试将最成功或最快的领域配置为第一,例如,您可以将您的领域顺序更改为saltedsha512jparelm
,ldapRealm
:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="roleRepository,roleRightRepository,rightRepository,userRepository">
<property name="realms">
<list>
<ref local="SaltedSha512JPARealm"/>
<ref local="ldapRealm"/>
</list>
</property>
<property name="authenticator.authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>
</property>
</bean>
上述定义是错误的。定义如下
<property name="authenticator.authenticationStrategy" ref="authcStrategy"/>
并分别定义下面的bean定义
<bean id="authcStrategy" class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>
然后它将按预期工作谢谢您的回答。我的问题是,即使在对
stuartldap服务器
()成功进行身份验证之后,它也会尝试使用saltedsha512jparelm
对数据库进行身份验证。这就是为什么我认为我的配置有问题。知道ldapRealm是首先添加的,它不应该发生,对吗?你是对的。看来,modulerarelauthenticator
的设计目的是让它始终尝试通过所有领域对用户进行身份验证。刚刚更新了答案。嗨,很抱歉反应太晚。我尝试使用不同的令牌,并根据我在UI上放置的组合框创建它们,就像MS windows登录切换域一样。这是我现在的快速修复方法。我将在下一次迭代中尝试这个解决方案,因为截止日期已经到了。因此,该修复程序对用户界面人员和管理层来说效果良好。
<property name="authenticator.authenticationStrategy" ref="authcStrategy"/>
<bean id="authcStrategy" class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>