I\'m using spring security with REST, and I\'m using the URL (/logout
) as an endpoint for my logout method. But after calling this method, it redirect me to (
You might want to try this
http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/thisistomisleadlogoutfilter"));
This effectively redirects /thisistomisleadlogoutfilter to login?logout. As such you should be able to use /logout instead
Following code works for me (notice that it doesn't have logout().disable()
)
http.logout().permitAll();
http.logout().logoutSuccessHandler((new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)));
I used this:
@ResponseStatus(HttpStatus.NO_CONTENT)
@PostMapping(value = "/oauth/revoke")
public void revokeToken(Authentication authentication) {
ofNullable(authentication).ifPresent(auth -> {
OAuth2AccessToken accessToken = tokenStore.getAccessToken((OAuth2Authentication) auth);
ofNullable(accessToken).ifPresent(oAuth2AccessToken -> {
ofNullable(oAuth2AccessToken.getRefreshToken()).ifPresent(tokenStore::removeRefreshToken);
tokenStore.removeAccessToken(accessToken);
});
});
}
From this gist
Which worked perfectly. I recommend doing this over the logout() override primarily because it (well, it works, but other than that) preserves the oauth2 basic flow (/oauth/revoke) instead of using /logout or similar.
Hope that helps!
for logoutSuccessXXX() action, do not forget to add permitAll() since the cookie is cleared after the logout() method is called. So my sample solution is:
http
......
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/logoutSuccess")
**.permitAll()**
Foo those who use XML config, here is the equivalent snippet for the one given by Tahir Akhtar.
Within <http>
element, configure the <logout>
element as follows:
<logout
logout-url = "/some/path/for/logout"
invalidate-session = "true"
delete-cookies = "JSESSIONID"
success-handler-ref = "httpStatusReturningLogoutSuccessHandler"
/>
And define httpStatusReturningLogoutSuccessHandler
bean as follows:
<bean
id = "httpStatusReturningLogoutSuccessHandler"
class = "org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler"
/>
Use this method:
.logout().logoutSuccessUrl("enter address here where you want to go after logout")