2013-01-14 39 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" /> 
相关问题