我创建了一个简单的 spring 用户应用程序。我正在使用 Tomcat 进行部署。这是我的 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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ContactSync</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>/user/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
<!-- <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping> -->
</web-app>
我的 spring-security.xml
<?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:security="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/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<!-- This is where we configure Spring-Security -->
<security:http auto-config="true" use-expressions="true" access-denied-page="/user/denied.do" >
<security:intercept-url pattern="/" access="permitAll"/>
<security:intercept-url pattern="/user/login.do" access="permitAll"/>
<!-- Not using admin role right now -->
<!-- <security:intercept-url pattern="/user/admin" access="hasRole('ROLE_ADMIN')"/> -->
<security:intercept-url pattern="/user/common.do" access="hasRole('ROLE_USER')"/>
<security:form-login
login-page="/user/login.do"
authentication-failure-url="/user/login.do?error=true"
default-target-url="/user/common.do"/>
<security:logout
invalidate-session="true"
logout-success-url="/user/login.do"
logout-url="/user/logout.do"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="abcd" password="abcd" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
我的图书馆是:
antlr-2.7.6.jar
antlr-runtime-3.0.1.jar
commons-collections-3.1.jar
commons-dbcp-1.4.jar
commons-logging-1.1.1.jar
commons-pool-1.5.6.jar
dom4j-1.6.1.jar
gson-2.2.1.jar
hibernate3.jar
hibernate-annotations-3.5.1-Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
j2ee.jar
javassist-3.4.GA.jar
javax.persistence-2.0.2.jar
jstl-1.2.jar
jta-1.1.jar
mysql-connector-java-5.1.5.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context.support-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.jdbc-3.0.1.RELEASE-A.jar
org.springframework.orm-3.0.1.RELEASE-A.jar
org.springframework.transaction-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
slf4j-api-1.5.6.jar
slf4j-simple-1.5.6.jar
spring-aop-3.0.1.RELEASE-sources.jar
spring-security-acl-3.0.1.RELEASE.jar
spring-security-acl-3.0.1.RELEASE-sources.jar
spring-security-cas-client-3.0.1.RELEASE.jar
spring-security-cas-client-3.0.1.RELEASE-sources.jar
spring-security-config-3.0.1.RELEASE.jar
spring-security-config-3.0.1.RELEASE-sources.jar
spring-security-core-3.0.1.RELEASE.jar
spring-security-core-3.0.1.RELEASE-sources.jar
spring-security-ldap-3.0.1.RELEASE.jar
spring-security-ldap-3.0.1.RELEASE-sources.jar
spring-security-openid-3.0.1.RELEASE.jar
spring-security-openid-3.0.1.RELEASE-sources.jar
spring-security-samples-tutorial-3.0.1.RELEASE.war
spring-security-samples-tutorial-3.0.1.RELEASE-sources.jar
spring-security-taglibs-3.0.1.RELEASE.jar
spring-security-taglibs-3.0.1.RELEASE-sources.jar
spring-security-web-3.0.1.RELEASE.jar
spring-security-web-3.0.1.RELEASE-sources.jar
standard-1.1.2.jar
yuicompressor-2.4.8pre.jar
尝试访问登录文件时出现 404 错误。但是我可以访问欢迎文件。任何人都可以帮我解决这个问题。
Error:
HTTP Status 404 - /Learning/WEB-INF/jsp/loginpage.jsp
type Status report
message /Learning/WEB-INF/jsp/loginpage.jsp
description The requested resource (/Learning/WEB-INF/jsp/loginpage.jsp) is not available.
我的登录注销控制器:
package com.contact.test.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping("/user")
@Controller
public class LoginLogoutController{
@RequestMapping(value = "/login.do", method = RequestMethod.GET)
public String getLoginPage(@RequestParam(value="error", required=false) boolean error,
ModelMap model) {
// Add an error message to the model if login is unsuccessful
// The 'error' parameter is set to true based on the when the authentication has failed.
// We declared this under the authentication-failure-url attribute inside the spring-security.xml
/* See below:
<form-login
login-page="/user/login"
authentication-failure-url="/user/login?error=true"
default-target-url="/user/common"/>*/
if (error == true) {
// Assign an error message
model.put("error", "You have entered an invalid username or password!");
} else {
model.put("error", "");
}
// This will resolve to /WEB-INF/jsp/loginpage.jsp
return "loginpage";
}
/**
* Handles and retrieves the denied JSP page. This is shown whenever a regular user
* tries to access an admin only page.
*
* @return the name of the JSP page
*/
@RequestMapping(value = "/denied.do", method = RequestMethod.GET)
public String getDeniedPage() {
// This will resolve to /WEB-INF/jsp/deniedpage.jsp
return "deniedpage";
}
}
项目结构为:
/Learning/WebContent
/Learning/WebContent/META-INF
/Learning/WebContent/WEB-INF
/Learning/WebContent/WEB-INF/jsp
/Learning/WebContent/WEB-INF/jsp/adminpage.jsp
/Learning/WebContent/WEB-INF/jsp/commonpage.jsp
/Learning/WebContent/WEB-INF/jsp/deniedpage.jsp
/Learning/WebContent/WEB-INF/jsp/loginpage.jsp
/Learning/WebContent/WEB-INF/jsp/userForm.jsp