在DispatcherServlet中找不到具有URI [/ pms / j_spring_security_check]的HTTP请求的映射,其名称为' appServlet'

时间:2014-06-20 06:25:12

标签: spring-mvc spring-security

我开发了一个Spring应用程序,并在其中实现了Spring安全集成到Login和Logout功能。我使用Spring安全性和xml配置。但是当我登录系统时,它显示404给我。控制台告诉我,在DispatcherServlet中找不到带有URI [/ pms / j_spring_security_check]的HTTP请求的映射名称' appServlet'

但我无法理解错误。我错过了什么?

我的 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>

我的控制器方法:

@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;
    }

我的 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>

我的登录页面的

<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>

请帮帮我。

3 个答案:

答案 0 :(得分:14)

我终于通过修改我声明自定义登录表单页面的行来解决了这个问题。

已添加处理-url =&#34; / j_spring_security_check&#34;:

package sample;

import java.io.IOException;
import java.util.List;

public class MyConcreteManager implements MyManagerInterface<MyBean> {

   @Override
   //this fails
   public List<MyBean> sortAll(List<MyBean> array) throws IOException {
       return null;
      }

    @Override
   //this works
    public List<MyBean> sortAll2(List<MyBean> array) {
        return null;
    }

    @Override
   //this works
    public List<MyBean> sortAll3() {
        return null;
    }

}

到我的security-context.xml页面

答案 1 :(得分:1)

下面的代码解决了基于注释的安全配置的问题。 http.formLogin()。loginProcessingUrl(“/ j_spring_security_check”)...其他设置

答案 2 :(得分:-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>