Spring安全性没有加载给404的页面

时间:2017-02-05 12:53:40

标签: java spring spring-mvc spring-security

由于我正在学习弹簧安全性,我正在尝试下面的第一个例子,它给了我404错误。请在下面找到安全配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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">
    <security:http auto-config="true">
        <security:intercept-url pattern="/hello"
            access="ROLE_SCARVAREZ_MEMBER" />
    </security:http>
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user authorities="ROLE_SCARVAREZ_MEMBER"
                    name="car" password="scarvarez" />
                <security:user authorities="ROLE_SCARVAREZ_MEMBER"
                    name="mon" password="scarvarez" />
                <security:user authorities="ROLE_SCARVAREZ_MEMBER"
                    name="bea" password="scarvarez" />
                <security:user authorities="ROLE_SCARVAREZ_MEMBER"
                    name="andr" password="scarvarez" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

后来我有一个servlet如下

package com.security.test;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = { "/hello" })
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 2218168052197231866L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.getWriter().write("Hello World");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这里的一切都很顺利,除非我登录时,而不是hello world,它给了我404.在tomcat控制台上没有任何东西存在。只想知道问题可能是什么?

修改: - 请找到我的web.xml文件

<web-app>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>
    <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>

1 个答案:

答案 0 :(得分:-1)

当您使用Spring安全性时,您不必使用HttpServlet而是使用Spring。

所以你现在要做的是创建一个spring config xml文件,并使用注释作为“@Controller”来映射你的控制器类,使用“@RequestMapping”作为你的“/ hello”。

我会评论,但我还不能。