Spring security logout goes to j_spring_security_logout

南楼画角 提交于 2019-12-18 16:47:34

问题


In my web application when I tries to logout it goes to j_spring_security_logout instead of the given page. In my spring-security.xml page i have added

<logout logout-success-url="/login" delete-cookies="JSESSIONID" />

The problem is this worked earlier when I used spring security 3.1.4.RELEASE version. Now I'm using 3.2.2.RELEASE

I've tried the following also. Didn't work

<logout logout-url="/logout" delete-cookies="JSESSIONID" />

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

<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="/transaction-view"
        always-use-default-target="true" authentication-failure-url="/loginfailed" />
    <logout logout-url="/logout" logout-success-url="/login.jsp" delete-cookies="JSESSIONID" />
    <session-management invalid-session-url="/invalidSession.htm">
        <concurrency-control max-sessions="1"
            error-if-maximum-exceeded="true" /> <!--this will throw error to second login attempt -->
    </session-management>
    <!-- <custom-filter before="FORM_LOGIN_FILTER" ref="myFilter" /> -->
    <csrf />
</http>

<beans:bean id="customSecurityService"
    class="com.fg.monitoringtool.web.security.SecurityService"></beans:bean>
<beans:bean id="passwordEncoder"
    class="com.fg.monitoringtool.web.security.PasswordEncoderMD5"></beans:bean>


<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="customSecurityService">
        <password-encoder ref="passwordEncoder">
        </password-encoder>
    </authentication-provider>

</authentication-manager>

Thanks in advance.


回答1:


When you have Spring Security CSRF protection enabled, you must logout with POST:

<c:url var="logoutUrl" value="/logout"/>
<form action="${logoutUrl}" method="post">
  <input type="submit" value="Log out" />
  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>



回答2:


A better approach to use default logout url would be

<c:url var="logoutUrl" value="j_spring_security_logout"/>
<form action="${logoutUrl}" method="post">
  <input type="submit" value="Log out" />
  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>


来源:https://stackoverflow.com/questions/22604064/spring-security-logout-goes-to-j-spring-security-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!