无法在spring自定义登录页面上显示页面

时间:2013-01-14 11:48:00

标签: spring login spring-security

我在尝试为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

4 个答案:

答案 0 :(得分:1)

您需要为匿名用户授予登录页面的访问权限。将以下代码段添加到 http 元素

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

编辑。订单很重要。必须先插入 这一行:

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

答案 1 :(得分:0)

  1. 您的登录信息 - 它是什么? HTML? JSP?您需要编写登录页面的URI(例如/login.html),而不仅仅是/ login。

  2. 尝试将您的登录页面放在子目录(例如/ login)中,以便匿名用户可以看到它:

  3. &LT; http pattern =“/ login / **”security =“none”/&gt;

    然后

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

答案 2 :(得分: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>

答案 3 :(得分:0)

这应该是我猜的。

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