我正在开发一个使用 Spring Security 3.0.7 通过用户名/密码或使用 OpenID 对用户进行身份验证的 Web 应用程序。现在我需要能够禁用某些帐户。起初我找不到相关文档,但最后我发现了User.isEnabled():
指示用户是启用还是禁用。无法验证禁用的用户。
此标志的值在构造函数中给出。
使用表单进行身份验证时,它似乎工作正常。不幸的是,Spring 的 OpenID 似乎完全忽略了该标志。我尽可能多地登录,我可以在日志中看到:
调试 ossoOpenIDAuthenticationFilter -身份验证成功。更新 SecurityContextHolder 以包含:[org.springframework.security.openid.OpenIDAuthenticationToken@66348da1:主体:mypackage.UserInfo@ddd49b1b:用户名:cbada36792e42a3be5a5e0f77d14e918186c7e3f;密码保护]; 启用:假;AccountNonExpired:真;凭据非过期:真;AccountNonLocked:真;授予权限:ROLE_USER;凭证:[受保护];已认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffd148a:RemoteIpAddress:127.0.0.1;会话 ID:1arhd8er0sj1yynglq8linpnb;授予权限:ROLE_USER,属性:[]]
如何在禁用的帐户上成功进行身份验证?(如果我尝试锁定帐户,也是一样的。)
我错过了什么重要的东西吗?或者它只是一个错误?任何想法要寻找什么,还有什么要启用的日志记录?
我的 XML 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<global-method-security secured-annotations="enabled">
</global-method-security>
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/" access="permitAll" />
<!-- ... other pattens -->
<form-login
login-page="/"
authentication-success-handler-ref="loginSuccessHandler"
/>
<remember-me data-source-ref="dataSource" user-service-ref="myUserDetails"/>
<openid-login
user-service-ref="openIdAuth"
authentication-success-handler-ref="loginSuccessHandler"
authentication-failure-handler-ref="openIdFailureHandler"
>
<attribute-exchange>
<openid-attribute name="email" type="http://axschema.org/contact/email" required="true" />
<openid-attribute name="name" type="http://axschema.org/namePerson" />
</attribute-exchange>
</openid-login>
<logout success-handler-ref="logoutSuccessHandler"/>
</http>
<authentication-manager>
<authentication-provider ref="daoAuthenticationProvider"/>
</authentication-manager>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="myUserDetails"/>
<beans:property name="saltSource" ref="saltSource"/>
<beans:property name="passwordEncoder" ref="passwordEncoder"/>
<beans:property name="preAuthenticationChecks">
<beans:bean class="org.springframework.security.authentication.AccountStatusUserDetailsChecker"/>
</beans:property>
</beans:bean>
</beans:beans>
这myUserDetails
是我的自定义 bean,它从数据库加载用户并返回一个简单的自定义实现User
:
public class UserInfo
extends User
{
public UserInfo(UserEntity user)
{
super( user.getUserName(),
user.getPassword(),
!user.isDisabled(), // enabled
true, // non-expired
true, // credentials non-expired
!user.isLocked(), // non-locked
UserInfo.authorities(user) // my static method
);
// store some info for further reference here
// ...
}
// ...
}