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>

这是我的弹簧配置文件

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

4 回答 4

1

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

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

编辑。顺序很重要。必须在此行之前插入:

<intercept-url pattern="/*" access="ROLE_USER" />
于 2013-01-14T12:39:35.807 回答
0
  1. 您的登录名 - 它是什么?HTML?JSP?您需要编写登录页面的 URI(例如 /login.html),而不仅仅是 /login。

  2. 尝试将您的登录页面放在允许匿名用户查看的子目录(例如 /login)中:

< http模式="/login/**" 安全="无" />

接着

<security:form-login 
    login-page="/login/login.jsp" 
        ...
于 2013-01-14T12:11:52.337 回答
0

我猜应该这样做。

   <intercept-url pattern="/login.htm" access="permitAll()"/>
   <form-login login-page="/login.htm"                    
               authentication-failure-url = "/positionViewer/login.htm?login_error=1" />
于 2013-04-05T05:13:09.683 回答
0

你说你使用的是spring,所以我假设你使用的是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>
于 2013-01-14T12:18:48.330 回答