Spring security oauth2 退出登录怎么写
时间: 2023-09-25 10:16:39 浏览: 79
Spring Security OAuth2 提供了一个 `/logout` 接口来实现退出登录的功能。当用户访问这个接口时,会删除用户的 access token 和 refresh token。
要使用 `/logout` 接口,需要在 Spring Security 配置文件中进行如下配置:
```java
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.logout()
.logoutUrl("/logout") // 配置退出登录接口
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.permitAll()
.and()
// 其他配置...
}
}
```
上面的配置将 `/logout` 接口配置为退出登录接口,并配置了退出登录后的一些操作,例如使 HttpSession 失效、清除认证信息、删除 JSESSIONID 等。其中 `logoutSuccessUrl("/")` 表示退出登录成功后跳转到首页。
然后在前端页面中,可以通过发送 POST 请求到 `/logout` 接口来触发退出登录的操作:
```html
<form th:action="@{/logout}" method="post">
<input type="submit" value="退出登录"/>
</form>
```
这样就可以实现 Spring Security OAuth2 的退出登录功能了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)