根据我在Spring Security中的user.status字段进行身份验证的优雅方式

时间:2013-01-03 14:45:02

标签: authentication spring-security

我可以使用

验证用户身份
Authentication request = new UsernamePasswordAuthenticationToken(this.username, this.password);
            Authentication result = authenticationManager.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);

现在我必须选择user.status字段为“有效”的用户。

我想我可以在上面的代码之前加上一些行检查状态字段,

但我也认为如果我可以让Spring Security为我做这件事,它更优雅,更整洁。

但我不知道如何通过覆盖某些类或某些方法让SS为我做这件事?

感谢您的建议。

2 个答案:

答案 0 :(得分:0)

如果您只有两种状态(“有效”,“无效”),则可以使用原生 user.enabled 字段代替自定义user.status fielad。 AuthenticationManager.authenticate()方法将自动检查此字段(如果您使用DaoAuthenticationProvider之类的本机AuthenticationProvider)。查看standard User Schema

中的已启用属性

答案 1 :(得分:0)

您可以在弹簧安全配置中将以下SpEL添加到拦截网址。

<intercept-url pattern="/**" access="isFullyAuthenticated() and principal.status == 'active'/>
相关问题