【问题标题】:Spring-security :The page isn't redirecting properlySpring-security:页面没有正确重定向
【发布时间】:2013-02-08 22:35:05
【问题描述】:

我想在 m web 应用程序中使用 spring 中的 spring security 所以这里是配置:

这是 spring-security.xml :

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true" use-expressions="false">
    <intercept-url pattern="/**" access="ROLE_USER" />
    <form-login login-page="/authentication"  login-processing-url="/static  
              /j_spring_security_check" authentication-failure 
              url="/login?login_error=t" />

</http>

<authentication-manager>
  <authentication-provider>
     <jdbc-user-service id="userService"
       data-source-ref="DataSource"
       users-by-username-query="select name, password, true from person where name=?"
       authorities-by-username-query="select name,'ROLE_USER' from person where    
       name=?" />
  </authentication-provider>
</authentication-manager>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns  
 /javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">

  <display-name>OTV_JSF_PrimeFaces_Spring_Hibernate</display-name>

  <!-- Spring Context Configuration' s Path definition -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  /WEB-INF/applicationContext.xml
  /WEB-INF/spring-security.xml
  </param-value>
 </context-param>

 <!-- The Bootstrap listener to start up and shut down Spring's root  
   WebApplicationContext. It is registered to Servlet Container -->
 <listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <listener>
 <listener-class>
     org.springframework.web.context.request.RequestContextListener
 </listener-class>
 </listener>

<!-- Project Stage Level -->
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>

<!-- Welcome Page -->
<welcome-file-list>
  <welcome-file>/home.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF Servlet is defined to container -->
 <servlet>
 <servlet-name>Faces Servlet</servlet-name>
 <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<!-- Mapping with servlet and url for the http requests. -->
<servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
 <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
<servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
 <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

   <!-- 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>
</web-app>

这是应用程序:

当我运行应用程序时,此 URL 被打开 http://localhost:8089/MVNOONPProject/authentication 我得到这个错误:

 `The page isn't redirecting properly
  Firefox has detected that the server is redirecting the request for this address in 
  a way that will never complete.`

我确定这是 web.xml 的问题。但我没有找到解决方法。

提前谢谢你

【问题讨论】:

  • 您正在使用自定义登录页面的另一件事,如果您使用自定义登录页面,您的
  • @Kris 不,如果您定义 form-login 元素,这不会有任何区别。最好完全忘记auto-config,因为它只会引起混乱。

标签: spring web-applications spring-security


【解决方案1】:

通常只保护适当的网页是有意义的,这些网页将在此处呈现 JSF。当然,您不应该拦截所有 url,否则将无法登录。这假设您在 /authentication 下有一个工作登录页面。

<http auto-config="true" use-expressions="false">
    <intercept-url pattern="/**/*.faces" access="ROLE_USER" />
    <intercept-url pattern="/**/*.jsf" access="ROLE_USER" />
    <intercept-url pattern="/**/*.xhtml" access="ROLE_USER" />
    <form-login login-page="/authentication"  login-processing-url="/static  
              /j_spring_security_check" authentication-failure 
              url="/login?login_error=t" />

</http>

【讨论】:

  • 这个网址打开localhost:8089/MVNOONPProject/authentication 显示404错误
  • 好吧,您需要在login-page url(在您的情况下是/authentication)下有一个正确的登录页面。以下是一些创建方法的提示:stackoverflow.com/questions/7361513/spring-security-login-page。如果你跳过login-page 属性,spring 应该为你创建一个默认的。
  • 完全删除 login-page 听起来是这里的最佳选择。该问题在FAQ 中进行了描述(总是一个很好的地方查看:)),您还应该在日志中看到一条警告消息,表明您的登录页面似乎受到保护。
【解决方案2】:

尝试两件事

添加

在表单登录标签中添加 default-target-url

default-target-url='/home.xhtml'

您使用自定义登录页面的另一件事,如果您使用自定义登录页面,您的 http auto-config="true" 将其更改为 false

所以你的安全配置应该是这样的(login-processing-url 也不需要)

<http auto-config="false" use-expressions="false">
<intercept-url pattern="/**" access="ROLE_USER" />
 < intercept-url pattern="/authentication" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<form-login login-page="/authentication"   authentication-failure 
          url="/login?login_error=t" default-target-url='/home.xhtml'/>

【讨论】:

    【解决方案3】:

    那是因为,你的 spring 安全配置会循环重定向。

    试试这个,

    <http auto-config="true" use-expressions="false">
         <intercept-url pattern="/login.jsp*" filters="none"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/authentication"  login-processing-url="/static  
                  /j_spring_security_check" authentication-failure 
                  url="/login?login_error=t" />
    
    </http>
    

    编辑


    <http auto-config="true" use-expressions="false">
         <intercept-url pattern="/authentication" filters="none"/>
         <intercept-url pattern="/login.jsp*" filters="none"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/authentication"  login-processing-url="/static  
                  /j_spring_security_check" authentication-failure 
                  url="/login?login_error=t" />
    
    </http>
    

    【讨论】:

      【解决方案4】:

      由于 pattern="/**" 拦截了所有 URL 请求,包括登录页面本身,任何用户都必须登录才能访问登录页面。所以经过数小时的尝试,以下为我做了诀窍。

      <intercept-url pattern="/login**" access="ROLE_ANONYMOUS" />
      <intercept-url pattern="/resources/**" access="ROLE_ANONYMOUS, ROLE_USER, ROLE_ADMIN" />
      <intercept-url pattern="/**" access="ROLE_USER" />
      
      <form-login 
          login-page="/login" 
          default-target-url="/home"
          authentication-failure-url="/login?error=true" />
      

      注意,

      • intercept-url 标签的顺序
      • pattern="/**" 基本上拦截所有的url请求,包括css和图片文件等资源。这就是需要第二行的原因。

      其他答案非常接近,但不适用于 Spring MVC 3.2.3.RELEASE 版本

      我认为这可能会在将来导致其他问题,因此更好的方法可能是,

      <intercept-url pattern="/admin*" access="ROLE_ADMIN" />
      <intercept-url pattern="/user*" access="ROLE_USER, ROLE_ADMIN" />
      <form-login 
          login-page="/login" 
          default-target-url="/home"
          authentication-failure-url="/login?error=true" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多