我正在尝试构建一个基于 Spring 的 Web 应用程序,我想从配置一个基于存储在数据库表中的用户名和密码元组的简单身份验证系统开始。
据我了解,这可以使用 Spring 安全性轻松实现,但我无法让它工作。
以下是我的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/Servlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
跟随servlet-context.xml文件。bob 和 sam 用户用于测试目的。在我做对了之后,我将切换到基于 JDBC 的用户服务。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<sec:http use-expressions="true">
<sec:intercept-url pattern="/**" access="permitAll" />
<sec:form-login
login-page="/home.html"
login-processing-url="/j_spring_security_check"
authentication-failure-url="/login-error.html"
default-target-url="/welcome.html" />
<sec:logout logout-success-url="/home.html" />
</sec:http>
<sec:authentication-manager>
<sec:authentication-provider>
<sec:password-encoder hash="md5"/>
<sec:user-service>
<sec:user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="ROLE_SUPERVISOR, ROLE_USER" />
<sec:user name="sam" password="d1a5e26d0558c455d386085fad77d427" authorities="ROLE_USER" />
</sec:user-service>
</sec:authentication-provider>
</sec:authentication-manager>
<context:component-scan base-package="cz.dusanrychnovsky.whattoreadnext" />
<mvc:annotation-driven />
</beans>
这是我的家庭控制器。
@Controller
public class HomeController
{
@RequestMapping(value = "/home.html")
public String home() {
return "home";
}
@RequestMapping(value = "/login-error.html")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "home";
}
}
这是我基于百里香叶的观点。
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Contacts</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="content">
<h1>Welcome to the site!</h1>
<p th:if="${loginError}">Wrong user or password</p>
<form th:action="@{/j_spring_security_check}" method="post">
<label for="j_username">Email address</label>:
<input type="text" id="j_username" name="j_username" /> <br />
<label for="j_password">Password</label>:
<input type="password" id="j_password" name="j_password" /> <br />
<input type="submit" value="Log in" />
</form>
</div>
</body>
</html>
当我将 WAR 文件部署到本地 Tomcat 安装并访问http://localhost:8080/test/home.html
URL 时,主页可以正常打开。但是,当我填写提交给 的表单时http://localhost:8080/test/j_spring_security_check
,我收到了一个404 - The requested resource () is not available.
错误。
我究竟做错了什么?请多多包涵,因为我是 Spring MVC/Security 和 Thymeleaf 的新手。