spring注销时出现错误404
我正在尝试使用spring security v.4向我的应用程序添加登录功能。登录工作正常,但当我尝试注销时,出现错误404。Spring安全参考说明默认注销URL为/logout。我的应用程序部署在/app URL下,我尝试了以下URL localhost:8080/app/logout和localhost:8080/app/json/logout。我在堆栈上发现了一些类似的问题,但它们是关于使用CSRF保护时的情况,我不使用它。这是我的web.xml文件的一部分spring注销时出现错误404,spring,spring-security,http-status-code-404,logout,Spring,Spring Security,Http Status Code 404,Logout,我正在尝试使用spring security v.4向我的应用程序添加登录功能。登录工作正常,但当我尝试注销时,出现错误404。Spring安全参考说明默认注销URL为/logout。我的应用程序部署在/app URL下,我尝试了以下URL localhost:8080/app/logout和localhost:8080/app/json/logout。我在堆栈上发现了一些类似的问题,但它们是关于使用CSRF保护时的情况,我不使用它。这是我的web.xml文件的一部分 <conte
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/json-servlet.xml,
/WEB-INF/applicationContext.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>json</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>json</servlet-name>
<url-pattern>/json/*</url-pattern>
</servlet-mapping>
上下文配置位置
/WEB-INF/json-servlet.xml,
/WEB-INF/applicationContext.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener
json
org.springframework.web.servlet.DispatcherServlet
1.
json
/json/*
还有我的json-servlet.xml,其中是spring安全配置:
<context:component-scan base-package="test" />
<mvc:annotation-driven />
<security:http>
<security:intercept-url pattern="/**" access="hasRole('USER')" />
<security:form-login />
<security:logout />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="test" password="1" authorities="ROLE_USER, ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
提前感谢。根据Spring Security的第4版开始,CSRF默认启用,默认支持的唯一方法是POST,因此您可以使用如下表单调用它:
<form method="post" action="${pageContext.request.contextPath}/logout" id="form-logout">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
在使用Spring4时不要使用XML。别这样。使用基于java的配置,这些配置在示例或Spring Boot中都有。
<http>
<!-- ... -->
<csrf disabled="true"/>
</http>
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}
}