首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Spring Security实现登录页面,使其与Spring web flow协同工作?

如何使用Spring Security实现登录页面,使其与Spring web flow协同工作?
EN

Stack Overflow用户
提问于 2010-05-21 21:33:23
回答 4查看 19.4K关注 0票数 18

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

代码语言:javascript
复制
<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>

身份验证管理器类:

代码语言:javascript
复制
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的基本部分如下所示:

代码语言:javascript
复制
<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缺少条目

代码语言:javascript
复制
<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一起工作。

EN

回答 4

Stack Overflow用户

发布于 2010-05-30 18:52:08

这里的文档描述了为什么需要'springSecurityFilterChain‘。

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

代码语言:javascript
复制
<http auto-config="false" session-fixation-protection="none">
    <form-login login-page="/login.jsp" default-target-url="/home.htm" />
</http>

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

代码语言:javascript
复制
<authentication-provider>
   <user-service id="userDetailsService">
      <user password="password" name="username" authorities="ROLE_USER" />
   </user-service>
</authentication-provider

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

这些细节和一个简单的spring安全应用程序都是用A aimple web application with Spring Security - part 13给出的。

参考文档将设置springSecurityFilterChain描述为配置web.xml的第一部分:Security Namespace Configuration - Getting Started。(链接到OP使用的2.0.x版本。最新的3.1版是here。)

票数 2
EN

Stack Overflow用户

发布于 2010-05-31 19:37:40

从您的代码中,根本不清楚您是否有Spring Security配置。我想你不知道。

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

http://www.springframework.org/schema/beans“...跳过...">http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> 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 ...已跳过... http://www.springframework.org/schema/security

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

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

Stack Overflow用户

发布于 2010-05-26 20:04:50

您是否根据Spring Web Flow Reference GuideSecuring Flows一章将应用程序配置为使用Spring Web Flow

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2882474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档