用于多个登录页面的Spring 3.x配置 [英] Spring 3.x configuration for multiple login pages

查看:73
本文介绍了用于多个登录页面的Spring 3.x配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 3.1进行身份验证.

I'm using Spring 3.1 for authentication purpose.

我的要求:

  • 两个不同的登录页面.一个给客户,另一个给雇员.
  • 成功认证后,每个都会转发到各自成功的URL.

我的spring安全配置:

My spring security configuration:

<sec:http pattern="/resources/**" security="none" />
<sec:http auto-config="true">
    <sec:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <sec:intercept-url pattern="/customer/**" access="ROLE_CUSTOMER" />
    <sec:intercept-url pattern="/employee/**" access="ROLE_EMPLOYEE" />
</sec:http>

<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
    <sec:filter-chain-map path-type="ant">
        <sec:filter-chain pattern="/**"
            filters="authenticationProcessingFilterForCustomer,authenticationProcessingFilterForEmployee" />
    </sec:filter-chain-map>
</bean>

<bean id="authenticationProcessingFilterForCustomer"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManagerForCustomer" />
    <property name="filterProcessesUrl" value="/j_spring_security_check_for_customer" />
    <property name="authenticationSuccessHandler" ref="customerSuccessHandler" />
    <property name="authenticationFailureHandler" ref="customerFailureHandler" />
</bean>
<bean id="customerSuccessHandler"
    class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/customer/index.html" />
</bean>
<bean id="customerFailureHandler"
    class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/customer.html?login_error=1" />
</bean>
<bean id="authenticationManagerForCustomer"
    class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref bean="customCustomerAuthenticationProvider" />
        </list>
    </property>
</bean>
<bean id="customCustomerAuthenticationProvider" class="com.edu.CustomerCustomAuthenticationProvider">
    <property name="userDetailsService">
        <bean class="com.edu.CustomerUserDetailsService" />
    </property>
</bean>

<bean id="authenticationProcessingFilterForEmployee"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManagerForEmployee" />
    <property name="filterProcessesUrl" value="/j_spring_security_check_for_employee" />
    <property name="authenticationSuccessHandler" ref="employeeSuccessHandler" />
    <property name="authenticationFailureHandler" ref="employeeFailureHandler" />
</bean>
<bean id="employeeSuccessHandler"
    class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/employee/index.html" />
</bean>
<bean id="employeeFailureHandler"
    class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/employee.html?login_error=1" />
</bean>
<bean id="authenticationManagerForEmployee"
    class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref bean="customEmployeeAuthenticationProvider" />
        </list>
    </property>
</bean>
<bean id="customEmployeeAuthenticationProvider" class="com.edu.EmployeeCustomAuthenticationProvider">
    <property name="userDetailsService">
        <bean class="com.edu.EmployeeUserDetailsService" />
    </property>
</bean>

<sec:authentication-manager alias="authenticationManager">
    <sec:authentication-provider ref="customCustomerAuthenticationProvider" />
    <sec:authentication-provider ref="customEmployeeAuthenticationProvider" />
</sec:authentication-manager>

CustomAuthenticationProvider都实现了以下支持方法:

Both CustomAuthenticationProvider have implemented Support method as follows:

public boolean supports(Class<? extends Object> authentication) {
    return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}

启动应用程序后,尝试进行身份验证时,登录页面中显示的消息为:

After launching application, while trying to authenticate, the message displayed in login pages are:

您的登录尝试失败,请重试.
原因:找不到org.springframework.security.authentication.UsernamePasswordAuthenticationToken的AuthenticationProvider

Your login attempt was not successful, try again.
Reason: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken

我正在使用Spring 3.1.任何帮助表示赞赏.

I'm using Spring 3.1. Any help appreciated.

谢谢

推荐答案

我在grails中做过类似的事情,您需要做的是:

I have done similar things in grails, what you need is:

  1. 扩展UsernamePasswordAuthenticationToken,为员工和客户创建两个子类,例如EmployeeUsernamePasswordAuthenticationToken和CustomerUsernamePasswordAuthenticationToken
  2. 扩展UsernamePasswordAuthenticationFilter,以基于当前身份验证请求创建EmployeeUsernamePasswordAuthenticationToken或CustomerUsernamePasswordAuthenticationToken的不同实例
  3. 扩展Employee和custoner的AuthenticationProvider,创建两个类,例如EmployeeAuthenticationProvider和CustomerAuthenticationProvider,覆盖每个类的supports方法以支持其目标UsernamePasswordAuthenticationToken
  4. 您只需要一个authenticationManager,即可将两者都注册到其中
  5. 只需要一个AuthenticationSuccessHandler,您可以决定要在哪个URL中输入
  6. 我还创建了自己的AuthenticationEntryPoint实例以支持多入口点

这篇关于用于多个登录页面的Spring 3.x配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆