我正在开发一个具有spring
和maven
的项目,当我使用eclipse run on server
运行它时,一切正常。但是当我在maven install
上执行deploy
和tomcat
申请时。它显示了我正确的登录页面,但是当我提交登录表单时。我通过404
url
得到myWebApp/j_spring_security_check
。
我已经尝试过我能找到的关于这个主题的所有内容,但这一切都毫无帮助。
我正在添加web.xml
,applicationContext.xml
,spring-security.xml
,login-page.jsp
和pom.xml
问题
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myApp-v1.0</display-name>
<context-param>
<param-name>types</param-name>
<param-value>Single-Select</param-value>
</context-param>
<context-param>
<param-name>typeIds</param-name>
<param-value>scq</param-value>
</context-param>
<context-param>
<param-name>rangeSeparator</param-name>
<param-value>-</param-value>
</context-param>
<!-- Question shold be given to user for user information -->
<context-param>
<param-name>userInformationQuestion</param-name>
<param-value>Please fill following information for better search result.</param-value>
</context-param>
<!-- Answer shold be given to user for user information -->
<context-param>
<param-name>userInformationAnswers</param-name>
<param-value>Age,Gender,Profession</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</init-param>
<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/classes/spring-security.xml
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<!-- Processes application requests -->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>itemSearch</servlet-name>
<jsp-file>/item-search.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>itemSearch</servlet-name>
<url-pattern>/ItemSearch</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/login-page.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>itemType</servlet-name>
<jsp-file>/item-type.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>itemType</servlet-name>
<url-pattern>/ItemType</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.psd</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<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>
<welcome-file-list>
<welcome-file> /item-search.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd">
<bean id="applicationContextProvider" class="com.myApp.util.ApplicationContextProvider" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<context:spring-configured />
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.myApp" />
<bean id="meassageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/classes/message/message_en.properties"></property>
</bean>
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
</bean>
<bean id="rangeSeparator" class="java.lang.String">
<constructor-arg value="${rangeSeparator}"/>
</bean>
<!-- -->
<bean id="encoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"></bean>
<bean id="userDetailsService" class="com.myApp.auth.service.MyAppUserDetailsService"/>
<bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="passwordEncoder" ref="encoder" />
<property name="userDetailsService" ref="userDetailsService"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myAppDb"/>
<property name="maxActive" value="5"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize" value="-1" />
</bean>
</beans>
spring-security.xml
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<http auto-config="true" disable-url-rewriting="true">
<intercept-url pattern="/item-search.jsp" access="ROLE_ADMIN"/>
<intercept-url pattern="/ItemSearch" access="ROLE_ADMIN"/>
<intercept-url pattern="/ItemType" access="ROLE_ADMIN"/>
<intercept-url pattern="/DecisionTree" access="ROLE_ADMIN"/>
<intercept-url pattern="/CustomAttribute" access="ROLE_ADMIN"/>
<form-login
login-page="/login"
default-target-url="/ItemSearch"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<remember-me
user-service-ref="userDetailsService"
token-validity-seconds="1209600"
remember-me-parameter="remember-me"
data-source-ref="dataSource" />
<logout logout-success-url="/login"/>
</http>
<authentication-manager>
<authentication-provider ref="authenticationProvider">
</authentication-provider>
</authentication-manager>
</beans:beans>
login-page.jsp
<!doctype html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login | Page</title>
<!-- fab icon -->
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon">
<link rel="icon" href="/myApp/images/favicon.ico" type="image/x-icon">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="css/jquery-ui.css">
<link rel="stylesheet" href="css/iThing.css" />
<link rel="stylesheet" href="css/bootstrap.css">
<!-- Optional theme -->
<link rel="stylesheet" href="css/bootstrap-theme.css">
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/toolTip.css">
<!-- <link rel="stylesheet" href="css/jquery.autocomplete.css" type="text/css" />-->
<script src="http://wcetdesigns.com/assets/javascript/jquery.js"></script>
<script src="http://wcetdesigns.com/assets/javascript/jquery/cookie-plugin.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/jquery.cookie.js"></script>
</head>
<body class="loginbg" >
<div class="col-lg-12 login_header">
<img src="images/logo.jpg" alt="logo" class="logo" />
</div>
<div class="col-lg-8 logindiv effect2">
<div class="container-fluid login_title">
<h2>Login</h2>
</div>
<div class="container-fluid login_body">
<c:url value="/j_spring_security_check" var="loginUrl"/>
<form name="loginForm" id="loginForm" class="login_form" action="${loginUrl}" method='POST'>
<div class="form_elements">
<span class="formicons username"></span>
<input type="text" name="username" id="login-user_name" placeholder="Email ID" class="gradient_glossy" />
</div>
<div class="form_elements">
<span class="formicons password"></span>
<input type="password" id="login_user_password" name="password" placeholder="Password" class="gradient_glossy" />
</div>
<div class="form_elements" style="margin-bottom: 0px;">
<div class="form_left">
<input type="checkbox" name="remember-me" id="chkLoginRrememberMe"/> Remember Me
</div>
<div class="form_right">
<a href="#">Forgot Password</a>
</div>
</div>
<div class="alert alert-danger" id="login_error_message" style="display:none">
<p id="error_msg_para"></p>
</div>
<c:if test="${param.error != null}">
<script type="text/javascript">
$("#error_msg_para").text("Invalid username or password!");
$("#login_error_message").show();
</script>
</c:if>
<div class="form_elements">
<button type="button" id="btnLoginSubmit" class="login_btn btn gradient_brown" name="submit" onclick="javascript:formSubmit()"> Login</button> <div id='result'></div>
</div>
</form>
<script>
function formSubmit() {
var username = $("#login-user_name").val();
var password = $("#login-user_password").val();
if(username === "" || password === ""){
$("#error_msg_para").text("Username or password can not be left empty!");
$("#login_error_message").show();
}
else if(username.indexOf("@")==-1 || username.indexOf(".com")==-1){
$("#error_msg_para").text("Invalid username format!");
$("#login_error_message").show();
}
else{
$("#btnLoginSubmit").attr('type','submit');
document.getElementById("loginForm").submit();
}
}
</script>
</div>
</div>
</body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myApp</groupId>
<artifactId>myApp</artifactId>
<version>v1.2</version>
<packaging>war</packaging>
<name>MyApp</name>
<properties>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.version>4.0.5.RELEASE</org.springframework.version>
<jstl.version>1.2</jstl.version>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>20040616</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<!-- JSTL taglib -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
问题出在web.xml
。我已将/rest/*
用于映射appServlet
并将其应用于所有调用,在将/rest/*
替换为/*
之后,应用程序正在完美运行。