使用Spring Security登录Spring MVC,获得404错误

时间:2014-01-31 10:43:28

标签: spring-security spring-4

我在Tomcat 7中运行项目时遇到404错误。

spring-security:

<beans xmlns:p="http://www.springframework.org/schema/p"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemalocation="http://www.springframework.org/schema/beans  
                           http://www.springframework.org/schema/beans/spring-beans.xsd  
                           http://www.springframework.org/schema/security  
                           http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    <!-- <beans:import resource="Employee-servlet.xml"/> -->

    <security:http use-expressions="true">
        <intercept-url pattern="/Springhib/index.jsp/" access="permitAll" />

        <intercept-url pattern="/Springhib/jsp/login.jsp"
            access="permitAll" />
        <form-login login-page="login.jsp" default-target-url="/welcome.jsp"
            authentication-failure-url="/error.jsp" />
        <logout invalidate-session="true" logout-url="/logout.jsp" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select username,password enabled from userlogin where username= ?" />
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

Employee-servlet.xml: 它包含dataSource(MySQL),hibernate事务管理器(Hibernate配置)和internalviewresolver

的web.xml:

<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_3_0.xsd"
    version="3.0">
    <display-name>SprinHib</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>
    <servlet>
        <servlet-name>Employee</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>
        <servlet-name>Employee</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/Employee-servlet.xml,
        /WEB-INF/spring-security.xml
        </param-value>

    </context-param>
    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>

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

项目结构

enter image description here

从索引页面我重定向到登录页面,同时在控制器中我实例化一个bean(POJO / Model)以便在下一页中进一步使用。 登录成功后,用户进入欢迎页面。

编辑:的index.jsp:

<c:redirect url="login"/> 

只包含一个autoredirect。

我在stackoverflow中搜索过以前的解决方案,但没有解决我的问题。

非常感谢任何帮助。

0 个答案:

没有答案