Spring Security:注销时为404

Tho*_*ers 29 java spring-mvc spring-security

当我尝试访问我的spring应用程序的注销URL时,我收到404错误并No mapping found for HTTP Request with URI [/logout] in DispatcherServlet with name 'mvc-dispatcher'在我的服务器日志中.

我已经尝试过调用j_spring_security_logout无法正常工作,Spring安全注销问题以及SO上的所有相关结果.

我包含了完整的配置文件,因为我还不太清楚Spring xml结构.

我的安全配置:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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.2.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <http pattern="/resources/**" security="none" />

    <http auto-config="true">
        <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/login" default-target-url="/"/>
        <logout logout-url="/logout" />
        <csrf />
    </http>

    <global-method-security secured-annotations="enabled" />

    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService" />
    </authentication-manager>

</beans:beans>
Run Code Online (Sandbox Code Playgroud)

我的web.xml是这样的:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>XYZ</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/*-config.xml</param-value>
    </context-param>

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

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

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

如何使注销页面正常工作?

Rob*_*nch 56

如果您使用CSRF注销,则必须执行POST.请参阅http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf-logout

  • 请注意,使用spring的`<form:form>`标签会自动为您自动添加`_csrf`隐藏标记. (4认同)

Tom*_*eba 9

从Spring 3.2迁移到4后我遇到了同样的问题,但是我想使用视图上的链接注销.

Spring doco(http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf-include-csrf-token-form)解释了如何在视图中执行此操作.

我在JSP中使用此片段进行注销:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="${pageContext.request.contextPath}/logout" method="POST">
    <input type="submit" value="Logout" />
</form:form>
Run Code Online (Sandbox Code Playgroud)


小智 7

试试这个,使用 HTTP.GET 注销

\n

WebSecurityConfigurerAdapter

\n
// In HttpSecurity configure\n...\n.logout()\n...\n.logoutRequestMatcher(new AntPathRequestMatcher("/logout", \xe2\x80\x9cGET\xe2\x80\x9d))\n...\n...\n\n
Run Code Online (Sandbox Code Playgroud)\n

HTML

\n
<a href="/logout">Logout</a>\n
Run Code Online (Sandbox Code Playgroud)\n