[英]Unable to display page on spring custom login page
我在嘗試為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
您需要授予匿名用戶訪問登錄頁面的權限。 將以下代碼段添加到您的http元素中
<intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
編輯。 順序很重要。 必須將其插入此行之前 :
<intercept-url pattern="/*" access="ROLE_USER" />
您的登錄名-這是什么? HTML? JSP? 您需要編寫登錄頁面的URI(例如/login.html),而不僅僅是/ login。
嘗試將您的登錄頁面放在子目錄(例如/ login)中,以使匿名用戶可以查看它:
<http pattern =“ / login / **” security =“ none” />
接着
<security:form-login
login-page="/login/login.jsp"
...
您說您正在使用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>
我猜應該這樣。
<intercept-url pattern="/login.htm" access="permitAll()"/>
<form-login login-page="/login.htm"
authentication-failure-url = "/positionViewer/login.htm?login_error=1" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.