18

我有一个使用 Spring 2.5.6 和 Spring Security 2.0.4 的 Web 应用程序。我已经实现了一个工作登录页面,它根据 Web 服务对用户进行身份验证。身份验证是通过定义一个自定义身份验证管理器来完成的,如下所示:


<beans:bean id="customizedFormLoginFilter"
    class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
    <custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
    <beans:property name="defaultTargetUrl" value="/index.do" />
    <beans:property name="authenticationFailureUrl" value="/login.do?error=true" />
    <beans:property name="authenticationManager" ref="customAuthenticationManager" />
    <beans:property name="allowSessionCreation" value="true" />
</beans:bean>

<beans:bean id="customAuthenticationManager"
    class="com.sevenp.mobile.samplemgmt.web.security.CustomAuthenticationManager">
    <beans:property name="authenticateUrlWs" value="${WS_ENDPOINT_ADDRESS}" />
</beans:bean>

身份验证管理器类:


public class CustomAuthenticationManager implements AuthenticationManager, ApplicationContextAware {

    @Transactional
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

                //authentication logic

        return new UsernamePasswordAuthenticationToken(principal, authentication.getCredentials(),
                grantedAuthorityArray);
    }

登录 jsp 的基本部分如下所示:


<c:url value="/j_spring_security_check" var="formUrlSecurityCheck"/>
<form method="post" action="${formUrlSecurityCheck}">
    <div id="errorArea" class="errorBox"> 
       <c:if test="${not empty param.error}">
          ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
      </c:if>
  </div>
    <label for="loginName">
        Username:           
        <input style="width:125px;" tabindex="1" id="login" name="j_username" />
    </label>

    <label for="password">
        Password:           
        <input style="width:125px;" tabindex="2" id="password" name="j_password" type="password" />
    </label>
    <input type="submit" tabindex="3" name="login" class="formButton" value="Login" />
</form>

现在的问题是应用程序应该使用Spring Web Flow。将应用程序配置为使用 Spring Web Flow 后,登录不再起作用 - 对“/j_spring_security_check”的表单操作导致空白页面没有错误消息。调整现有登录过程以使其与 Spring Web Flow 一起使用的最佳方法是什么?

编辑:没有错误消息,只是显示了一个空白页。它现在可以工作了 - 问题是 web.xml 缺少条目


<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>

由于尽管问题解决了,但我无法取消赏金,因此将赏金授予最有见地的答案或链接,这将有助于其他人配置 Spring Security 和 Web Flow 一起工作。

4

4 回答 4

2

这是描述为什么需要“springSecurityFilterChain”的文档。

springSecurityFilterChain 过滤器不是显式创建的,而是由<http>应用程序上下文中的 spring 安全模式元素隐式创建的

<http auto-config="false" session-fixation-protection="none">
    <form-login login-page="/login.jsp" default-target-url="/home.htm" />
</http>

创建的 bean 需要一个身份验证提供程序,因此我们提供了一个

<authentication-provider>
   <user-service id="userDetailsService">
      <user password="password" name="username" authorities="ROLE_USER" />
   </user-service>
</authentication-provider

随着这些更改到位,包括已编辑问题中描述的安全过滤器配置,页面将正确呈现,并且配置的安全堆栈将在每个提供的页面上到位。

这些细节和一个简单的 spring-security 应用程序在一个具有 Spring Security 的目标 Web 应用程序 - 第 13 部分中给出。

参考文档将设置描述springSecurityFilterChain为配置 web.xml 的第一部分:安全命名空间配置 - 入门。(链接到OP使用的2.0.x版本。最新版本3.1在这里。)

于 2010-05-30T10:52:08.263 回答
1

从您的代码中不清楚您是否有 Spring Security 配置。我假设你没有。

  • 您必须在配置中添加安全名称空间。像这样的东西

    
    <beans xmlns="http://www.springframework.org/schema/beans" ... skipped ...
        xmlns:security="http://www.springframework.org/schema/security"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        ... skipped ...
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    

  • 您需要配置 Spring Security。例如:

    
    <security:http once-per-request="false" auto-config="false">
        <security:form-login login-page="path/to/your/login.jsp"/>
    </security:http>
    

  • 这可能足以让它“正常工作”,但我认为你应该看看实现你的AuthenticationProvider而不是 AuthenticationManager。在这种情况下,您将需要类似于此的配置:

    
    <bean class="my.company.project.MyAuthProvider"
          autowire="byType">
        <security:custom-authentication-provider/>
    </bean>
    

于 2010-05-31T11:37:40.177 回答
0

我不得不将我的表单名称从 j_security_check 更改为 glassfish 上的其他名称,因为它似乎可以解释该 url 本身。

于 2010-05-27T03:42:55.487 回答
0

您是否根据Spring Web Flow 参考指南的Securing Flows章节配置您的应用程序以使用 Spring Web Flow ?

于 2010-05-26T12:04:50.063 回答