Web*_*ser 8 spring-mvc spring-security
我开发了一个Spring应用程序,并在其中实现了Spring安全集成到Login和Logout功能.我使用Spring安全性和xml配置.但是当我登录系统时,它显示404给我.控制台告诉我,在名为'appServlet'的DispatcherServlet中找不到带有URI [/ pms/j_spring_security_check]的HTTP请求的映射
但我无法理解错误.我错过了什么?
我的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">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/spring-security.xml</param-value>
</context-param>
<!-- Spring Security -->
<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>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我的控制器方法:
@RequestMapping(value="/login", method = RequestMethod.GET)
public ModelAndView printWelcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Spring security allows you");
modelAndView.setViewName("loginForm");
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
我的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-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="/loginForm"/>
<intercept-url pattern="/" access="ROLE_USER" />
<form-login login-page="/loginForm"
default-target-url="/login" always-use-default-target="true"
authentication-failure-url="/loginForm?login_error=1" />
<logout logout-success-url="/loginForm" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="a2ztechguide" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
Run Code Online (Sandbox Code Playgroud)
和 我的登录页面:
<body>
<table>
<tr>
<td valign="top"><c:if test="${not empty param.login_error}">
<font color="red"> Invalid user name or password, try again.
<br /> <br />
</font>
</c:if>
<form name="login_form"
action="<c:url value='j_spring_security_check'/>" method="POST">
<div>
<table width="40%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<table border="0" cellspacing="0" cellpadding="4" width="40%">
<tr>
<td colspan="2">Custom Login Form
<hr width="100%" size="1" noshade align="left">
</td>
<td></td>
</tr>
<tr>
<td width="80">Username</td>
<td valign="top" align="left"><input type='text'
id='username' size="30" maxlength="40" name='j_username'
value='<c:if test="${not empty param.login_error}">
<c:out value="${SPRING_SECURITY_LAST_USERNAME}"/>
</c:if>' />
</td>
</tr>
<tr>
<td width="80">Password</td>
<td valign="top" align="left"><input type='password'
name='j_password' size="30" maxlength="30"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form></td>
</tr>
</table>
</body>
Run Code Online (Sandbox Code Playgroud)
请帮我.
Eug*_*gen 14
我终于通过修改我声明自定义登录表单页面的行来解决了这个问题.
添加了processing-url ="/ j_spring_security_check":
<form-login login-page="/login" default-target-url="/admin" login-processing-url="/j_spring_security_check" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password"/>
Run Code Online (Sandbox Code Playgroud)
到我的security-context.xml页面
cod*_*ent -2
尝试将 Web xml 中的 servlet 映射更改为:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9889 次 |
| 最近记录: |