spring security的logout功能出现404

查看HttpSecurity的logout().logoutUrl()的API

The URL that triggers log out to occur (default is "/logout"). If CSRF protection is enabled (default), then the request must also be a POST. This means that by default POST "/logout" is required to trigger a log out. If CSRF protection is disabled, then any HTTP method is allowed. 
It is considered best practice to use an HTTP POST on any action that changes state (i.e. log out) to protect against CSRF attacks. If you really want to use an HTTP GET, you can use logoutRequestMatcher(new AntPathRequestMatcher(logoutUrl, "GET"));

上面说:默认情况下”/logout”必须使用POST提交才可以起作用
原因:CSRS功能默认开启了

CSRF功能是否开启:

1)开启:使用POST提交,注意token了

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

2)没有开启:

需要手动关闭csrf功能:
使用HTTPSecurity的csrf().disable();关闭
此时既可以使用Get方法也可以使用Post方法提交

在CSRF功能开启的情况下使用GET 去logout

该方法为HTTPSecurity的方法
logoutRequestMatcher(new AntPathRequestMatcher(logoutUrl, “GET”))
此时指定了提交方法去限制logout

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值