2013-01-14 73 views
0

我在嘗試爲Spring Web應用程序創建自定義登錄頁面時遇到問題。無法在春季自定義登錄頁面上顯示頁面

默認登錄表單正常工作。 但是當我添加

**<form-login login-page="/login" />** 

應用程序沒有錯誤,但「無法顯示網頁」運行顯示出來。

你有什麼想法嗎? 感謝

這裏是我的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_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>testweb</display-name> 

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

    <!-- 
     - Loads the root application context of this web app at startup. 
    --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- 
     - Location of the XML file that defines the root application context 
     - Applied by ContextLoaderListener. 
     --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext-security.xml 
     </param-value> 
    </context-param> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

     <load-on-startup>1</load-on-startup> 
    </servlet>  

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


</web-app> 

這裏是我的Spring配置文件

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 

    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Scans within the base package of the application for @Components to configure as beans --> 
    <!-- @Controller, @Service, @Configuration, etc. --> 
    <context:component-scan base-package="myPACKAGE" /> 
    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 


<!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 
</beans> 

這裏是我的春天安全文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" 

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

    <http auto-config="true"> 
     <intercept-url pattern="/*" access="ROLE_USER" /> 
     **<form-login login-page="/login" />** 

    </http> 

    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="myuser" password="mypwd" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

自定義登錄頁面WEB-INF/views/login.jsp內的login.jsp

+0

你能告訴我們,如果你得到'資源未找到'或安全相關異常 – Greg

回答

0
  1. 您的登錄名 - 這是什麼? HTML? JSP?您需要編寫登錄頁面的URI(例如/login.html),而不僅僅是/ login。

  2. 嘗試把你的登錄頁面的子目錄(如/登錄),允許匿名用戶看到它:

< HTTP模式=「/登錄/ **」安全=」無」 />

然後

<security:form-login 
    login-page="/login/login.jsp" 
     ... 
0

你說你正在使用的彈簧,所以我會假設你使用Spring MVC的,你有沒有加入

<!-- selects a static view for rendering without the need for an explicit controller --> 
<mvc:view-controller path="/login" view-name="login"/> 

到您的webmvc配置。

而且更改登錄頁面映射您可以添加

<definition extends="default" name="/login"> 
    <put-attribute name="body" value="/WEB-INF/views/login.jspx"/> 
</definition> 
1

你需要給匿名用戶登錄頁面的訪問。下面的代碼片段添加到您的HTTP元素

<intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 

編輯。訂單很重要。它必須之前插入這一行:

<intercept-url pattern="/*" access="ROLE_USER" /> 
0

這應該做我猜。

<intercept-url pattern="/login.htm" access="permitAll()"/> 
    <form-login login-page="/login.htm"      
       authentication-failure-url = "/positionViewer/login.htm?login_error=1" /> 
相關問題