【发布时间】: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? -
我调用的是
/helloURL。调用和登录是同一个URL,成功后调用Hello World -
@Andremoniy:也可以使用
@Controller,但这会是个问题吗? -
会的,因为你必须在web.xml文件中注册servlet,但它根本与Spring无关
-
@Andremoniy 我也添加了我的 web.xml 文件。由于我还没有配置
DispatcherServlet,并且在Servlet上的注释的帮助下,我是否需要在web.xml中注册我的文件?
标签: java spring spring-mvc spring-security