Spring security - Disable logout redirect

后端 未结 7 1808
北恋
北恋 2021-01-17 09:59

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 (

相关标签:
7条回答
  • 2021-01-17 10:21

    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

    0 讨论(0)
  • 2021-01-17 10:23

    Following code works for me (notice that it doesn't have logout().disable())

    http.logout().permitAll();
    http.logout().logoutSuccessHandler((new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)));
    
    0 讨论(0)
  • 2021-01-17 10:28

    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!

    0 讨论(0)
  • 2021-01-17 10:31

    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()**
    
    0 讨论(0)
  • 2021-01-17 10:32

    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"
    />
    
    0 讨论(0)
  • 2021-01-17 10:46

    Use this method:

    .logout().logoutSuccessUrl("enter address here where you want to go after logout")
    
    0 讨论(0)
提交回复
热议问题