【问题标题】:Access a controller before authentication Spring Security在身份验证之前访问控制器 Spring Security
【发布时间】:2013-01-02 19:46:19
【问题描述】:

我一直在尝试为多个登录页面实施解决方案。我目前有一个唯一的 LoginController,它所做的只是在有人请求 /app_name/login.htm 时检索登录 jsp。我希望保持这种状态,但还要添加另外两个位置:/app_name/customers/login.htm 和 /app_name/employees/login.htm,每个位置都有一个单独的控制器 CustomerLoginController 和 EmployeeLoginController。

所以我的想法是员工通过他们的 URL 和客户使用他们的 URL 进行访问,但是如果有人尝试访问旧的 login.htm,控制器会将他/她重定向到他们各自的登录,默认使用存储的 cookie 和客户。

对我来说这听起来不错,但是当我尝试访问 /app_name/customers/login.htm 或 /app_name/employees/login.htm 时,它只会在我未通过身份验证时将我重定向到 login.htm。

我真的不知道为什么它没有解决它们。任何意见、建议、指南、教程、示例代码或链接都会有所帮助。

我正在做的项目有这个配置

Web.xml

<!-- Spring Security -->
<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

servlet-config.xml

<!-- Controllers Mapping -->
<context:component-scan base-package="com.company.project.controllers">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

security-context.xml

<sec:http auto-config="false" entry-point-ref="authenticationProcessingFilterEntryPoint" access-denied-page="/warning/accessDenied.htm" >
    <sec:intercept-url pattern="/employees/login.htm" filters="none" />
    <sec:intercept-url pattern="/customers/login.htm" filters="none" />
    <sec:intercept-url pattern="/login**" filters="none" />
</sec:http>
<bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
    <property name="loginFormUrl" value="/login.htm" />
    <property name="forceHttps" value="false" />
</bean>
<bean id="authenticationProcessingFilter" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
    <property name="authenticationFailureUrl" value="/login.htm?login_error=1"/>
    <property name="defaultTargetUrl" value="/home.htm"/>
    <property name="alwaysUseDefaultTargetUrl" value="true"/>
    <property name="filterProcessesUrl" value="/j_spring_security_check"/>
</bean>

PD:使用 Spring 2.5.4 和 Spring Security 2.0.4 -_- 我知道,但这是一个相当大的项目,并且已经投入生产一段时间了

【问题讨论】:

  • 有人吗?这个我还真没想到

标签: spring servlets spring-mvc spring-security


【解决方案1】:

可以使用 2 个单独的 http 声明来完成多个登录页面:

<http pattern="/customers/**" authentication-manager-ref="customerAuthenticationManager">
    <form-login login-page="/customers/login" default-target-url="/customers" login-processing-url="/customers/j_spring_security_check"/>
    <logout logout-url="/customers/logout"/>
    ...
</http>

<http pattern="/employees/**" authentication-manager-ref="employeeAuthenticationManager">
    <form-login login-page="/employees/login" default-target-url="/employees" login-processing-url="/employees/j_spring_security_check"/>
    <logout logout-url="/employees/logout"/>
    ...
</http>

【讨论】:

    猜你喜欢
    • 2014-02-04
    • 2020-11-11
    • 2019-04-08
    • 1970-01-01
    • 2012-09-04
    • 2014-09-04
    • 2013-01-15
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多