Java 将SpringSecurity3.x配置为具有多个入口点
我一直在使用SpringSecurity3.x为我的项目处理用户身份验证,到目前为止,它工作得完美无缺 我最近收到了一个新项目的需求。在这个项目中,它需要两组用户身份验证:一组根据LDAP对员工进行身份验证,另一组根据数据库对客户进行身份验证。我对如何在SpringSecurity中配置它有点困惑 我最初的想法是创建一个具有以下字段的登录屏幕:-Java 将SpringSecurity3.x配置为具有多个入口点,java,spring,authentication,spring-security,forms-authentication,Java,Spring,Authentication,Spring Security,Forms Authentication,我一直在使用SpringSecurity3.x为我的项目处理用户身份验证,到目前为止,它工作得完美无缺 我最近收到了一个新项目的需求。在这个项目中,它需要两组用户身份验证:一组根据LDAP对员工进行身份验证,另一组根据数据库对客户进行身份验证。我对如何在SpringSecurity中配置它有点困惑 我最初的想法是创建一个具有以下字段的登录屏幕:- 单选按钮字段-供用户选择他们是员工还是客户 j_用户名user字段 j_密码密码字段 如果用户选择“employee”,那么我希望Spring S
- 单选按钮字段-供用户选择他们是员工还是客户
user字段j_用户名
密码字段j_密码
/j\u spring\u security\u check
,我无法将单选按钮字段发送给实现的自定义身份验证提供商。我最初的想法是,我可能需要两个表单提交URL,而不是依赖默认的/j_spring\u security\u check
。每个URL都将由不同的身份验证提供程序处理,但我不确定如何在SpringSecurity中配置它
我知道在SpringSecurity中,我可以配置回退身份验证,例如,如果LDAP身份验证失败,那么它将回退到数据库身份验证,但这不是我在这个新项目中的目标
有人可以分享我应该如何在SpringSecurity3.x中配置它吗
多谢各位
更新-01-28-2011-@EasyAngel的技术 我正在努力做到以下几点:-
- 员工表单登录提交给
/j\u spring\u security\u check\u\u员工
- 客户表单登录提交给
/j\u spring\u security\u check\u以检查客户
错误404:SRVE0190E:找不到文件:/j_Spring_Security_check_for_employee
。我的直觉是springSecurityFilterChain
bean没有正确连接,因此根本没有使用我的自定义过滤器
顺便说一句,我正在使用WebSphere,并且在服务器中设置了com.ibm.ws.webcontainer.invokefilterscompatibility=true
属性。我可以毫无问题地点击默认的/j\u spring\u security\u check
以下是我的完整安全配置:-
我在这里开始悬赏,因为我似乎已经有好几天没法让它工作了。。。挫折是一个词。我希望有人能指出这个问题,或者你能给我一个更好或者更干净的方法来处理这个问题(在代码中)
我正在使用SpringSecurity3.x
多谢各位
更新01-29-2011-@Ritesh的技术 好吧,我设法让@Ritesh的方法非常接近我想要的。我有一个单选按钮,允许用户选择他们是客户还是员工。这种方法似乎运行得相当好,但有一个问题
- 如果员工使用正确的凭据登录,则允许他们进入按预期工作
- 如果员工使用错误的凭据登录,则不允许他们进入按预期工作
- 如果客户使用正确的凭据登录,则允许他们进入按预期工作
- 如果客户使用错误的凭据登录,身份验证将退回到员工身份验证不起作用。这是有风险的,因为如果我选择客户身份验证,并在其上打上员工凭证,它也将允许用户进入,而这不是我想要的
这是我的更新配置。这必须是一些非常小的调整,我需要做的,以防止认证回落,但我似乎无法找出它现在
多谢各位
更新-解决@Ritesh的技术问题
好的,我想我已经解决了这个问题。我没有让EmployeeCustomAuthenticationProvider
依赖默认的UsernamePasswordAuthenticationToken
,而是为它创建了EmployeeUsernamePasswordAuthenticationToken
,就像我为CustomerCustomAuthenticationProvider
创建的CustomerUsernamePasswordAuthenticationToken
一样。然后,这些提供程序将覆盖支持()
:-
CustomerCustomAuthenticationProvider类
@Override
public boolean supports(Class<? extends Object> authentication) {
return (CustomerUsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (EmployeeUsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
...
UsernamePasswordAuthenticationToken authRequest = null;
if ("customer".equals(request.getParameter("radioAuthenticationType"))) {
authRequest = new CustomerUsernamePasswordAuthenticationToken(username, password);
}
else {
authRequest = new EmployeeUsernamePasswordAuthenticationToken(username, password);
}
setDetails(request, authRequest);
return super.getAuthenticationManager().authenticate(authRequest);
}
。。。还有瓦拉!经过几天的挫折之后,它现在工作得非常好
希望这篇文章能够帮助那些和我在这里做同样事情的人。你可以将这些信息存储在数据库中。例如,你可以
<sec:http auto-config="true">
...
<sec:custom-filter ref="authenticationProcessingFilterForCustomer" after="FIRST"/>
<sec:custom-filter ref="authenticationProcessingFilterForEmployee" after="FIRST"/>
</sec:http>
<security:http>
<security:custom-filter position="FORM_LOGIN_FILTER" ref="customFormLoginFilter" />
</security:http>
if (radiobutton_param value employee) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
setDetails(whatever);
return getAuthenticationManager().authenticate(authRequest);
} else if (radiobutton_param value customer) {
CustomerAuthenticationToken authRequest = new CustomerAuthenticationToken(username, password);
setDetails(whatever);
return getAuthenticationManager().authenticate(authRequest);
}
@Override
public boolean supports(Class<?> authentication) {
return (CustomerAuthenticationToken.class.isAssignableFrom(authentication));
}
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref='employeeCustomAuthenticationProvider ' />
<security:authentication-provider ref='customerCustomAuthenticationProvider ' />
</security:authentication-manager>