【问题标题】:Spring security not loading the page giving 404Spring Security 未加载给出 404 的页面
【发布时间】:2017-02-05 21:33:16
【问题描述】:

因为我正在学习弹簧安全性,所以我尝试了下面的第一个示例,它给了我 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();
        }
    }
}

这里一切正常,除了我登录时,它给了我 404,而不是 hello world。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>

【问题讨论】:

  • 你为什么在春天使用HttpServlet
  • 我调用的是/hello URL。调用和登录是同一个URL,成功后调用Hello World
  • @Andremoniy:也可以使用@Controller,但这会是个问题吗?
  • 会的,因为你必须在web.xml文件中注册servlet,但它根本与Spring无关
  • @Andremoniy 我也添加了我的 web.xml 文件。由于我还没有配置DispatcherServlet,并且在Servlet 上的注释的帮助下,我是否需要在web.xml 中注册我的文件?

标签: java spring spring-mvc spring-security


【解决方案1】:

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

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

我会发表评论,但我还不能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 2017-08-18
    • 2021-11-22
    • 2017-08-05
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多