我正在开发一个使用Spring Security 3.0.7通过用户名/密码或使用OpenID对用户进行身份验证的Web应用程序。现在我需要能够禁用某些帐户。起初我找不到相关的文档,但最后我发现了User.isEnabled():
指示用户是启用还是禁用。禁用的用户无法进行身份验证。
此标志的值在构造函数中给出。
使用表单进行身份验证时似乎工作正常。不幸的是,Spring的OpenID似乎完全忽略了旗帜。我尽可能多地记录,我可以在日志中看到:
DEBUG o.s.s.o.OpenIDAuthenticationFilter - 身份验证成功。更新SecurityContextHolder以包含:[org.springframework.security.openid.OpenIDAuthenticationToken@66348da1:Principal:mypackage.UserInfo@ddd49b1b:用户名:cbada36792e42a3be5a5e0f77d14e918186c7e3f;密码保护]; 已启用:false; AccountNonExpired:true; credentialsNonExpired:true; AccountNonLocked:true;授权机构:ROLE_USER;证书:[保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffd148a:RemoteIpAddress:127.0.0.1; SessionId: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
// ...
}
// ...
}
答案 0 :(得分:0)
我现在处理类似情况。
Spring Security基于过滤器链。如果你声明openid而不是opendId auth被添加到你的安全过滤器链(我不知道spring openid详细信息)。
当您的过滤器链处于openId步骤并且某人已登录时,它将返回auth成功而不会在UserDetails表中看到禁用状态。
第7.3章:docs.spring.io/spring-security/site/docs/3.0.x/reference/security-filter-chain.html
我使用Spring Social(对于FB等),当有人通过FB登录时,isDisabled()对我也不起作用。
解决方法是更改默认弹簧的openid实现,并在UserDetails中观察isDisabled()。
可能 此处:OpenIDAuthenticationProvider。这是一枪!我不知道spring openid
希望有所帮助。