Spring安全注销处理

时间:2015-04-09 07:15:41

标签: spring spring-security

根据Spring Security 4.0.0文件:

  

4.2.4注销处理

     

logout元素通过导航到a来添加对注销的支持   特定的URL。 默认的退出网址是/ logout ,但您可以进行设置   使用logout-url属性进行其他操作。更多信息   其他可用属性可以在命名空间附录中找到。

但是,在遵循文档中的安全设置后,URL /注销不会显示注销页面。相反,它显示

enter image description here

相反,URL /登录正常。

enter image description here

以下是我的设置:

Spring Framework 4.1.6
Spring Security 4.0.0

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Test8</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

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

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


</web-app>

安全-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <intercept-url pattern="/**" access="hasRole('USER')" />
        <form-login />
        <logout />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="aaa" password="111" authorities="ROLE_USER, ROLE_ADMIN" />
                <user name="bbb" password="222" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

8 个答案:

答案 0 :(得分:12)

Spring security automatically enables csrf, which automatically disabled GET logouts. You can fix this by disabling csrf protection by settings <csrf disabled="true"/> in the <http> , or just using a POST.

See http://docs.spring.io/spring-security/site/docs/4.0.1.RELEASE/reference/htmlsingle/#csrf-logout

答案 1 :(得分:5)

简单地说,将以下代码放在要注销的jsp中 -

<c:url var="logoutUrl" value="/j_spring_security_logout" />
    <form action="${logoutUrl}" id="logout" method="post">
        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
    </form>
    <a href="#" onclick="document.getElementById('logout').submit();">Logout</a>

bean配置文件中的对应条目 -

<security:logout logout-url="/j_spring_security_logout" logout-success-url="/whateverPageYouWant" invalidate-session="true" />

- 这对我来说是春天安全-4。*

答案 2 :(得分:2)

https://docs.spring.io/spring-security/site/docs/5.1.0.RELEASE/reference/htmlsingle/

处理注销

使用WebSecurityConfigurerAdapter时,注销功能将自动应用。默认设置是访问URL / logout将通过以下方式注销用户:

使HTTP会话无效 清理配置的所有RememberMe身份验证 清除SecurityContextHolder 重定向到/ login?登出 但是,与配置登录功能相似,您还可以使用各种选项来进一步自定义注销要求:

downloadTaskWithResumeData:
  1. 提供注销支持。使用WebSecurityConfigurerAdapter时会自动应用。

  2. 触发注销的URL发生(默认为/ logout)。如果启用了CSRF保护(默认),则请求也必须是POST。有关更多信息,请查阅JavaDoc。

  3. 注销后重定向到的URL。默认值为/ login?logout。有关更多信息,请查阅JavaDoc。

  4. 让我们指定一个自定义的LogoutSuccessHandler。如果指定此选项,则忽略logoutSuccessUrl()。有关更多信息,请查阅JavaDoc。

  5. 指定注销时是否使HttpSession无效。默认情况下是这样。在幕后配置SecurityContextLogoutHandler。有关更多信息,请查阅JavaDoc。

  6. 添加一个LogoutHandler。默认情况下,SecurityContextLogoutHandler被添加为最后一个LogoutHandler。

  7. 允许指定注销成功后要删除的Cookie的名称。这是显式添加CookieClearingLogoutHandler的快捷方式。

答案 3 :(得分:1)

  1. 注销网址为“ / j_spring_security_logout”,因此请相应地编辑视图
  2. CSRF默认情况下处于启用状态,这将要求每个POST请求(注销)具有CSRF令牌。因此,要么禁用CSRF(我不建议这样做),要么将注销框架内框成具有上述注销URL的操作以及带有CSRF令牌的隐藏输入,例如

答案 4 :(得分:0)

请注意,没有“退出页面”。 / logout是Spring的端点,让Spring知道应用程序要求注销用户,因此它会调用特定的处理程序。

将用户注销后,Spring会重定向到另一个页面,您可以在XML中配置“默认目标”。

答案 5 :(得分:0)

添加到Spring Security:

<logout
 logout-success-url="/anonymous.html"
 logout-url="/perform_logout"
 delete-cookies="JSESSIONID" />
http 标记下的

答案 6 :(得分:0)

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
   //...
   http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
  }
}

答案 7 :(得分:0)

使用Spring Security 4.2.13,我可以通过表单提交(POST方法)导航到注销URL而不是使用链接来完成这项工作。

我将<p><a href="<c:url value='/logout'/>">Log out</a></p>替换为

<form name='f' action='${pageContext.request.contextPath}/logout' method='POST'>
<input name="logout" type="submit" value="Log out" />
<input name="${_csrf.parameterName}" type="hidden"
    value="${_csrf.token}" />
</form>

在我的视图层中,这是一个JSP页面。这样,您将获得一个按钮而不是一个链接。 (在较早的Spring版本中,默认注销URL为“ / j_spring_security_logout”。)