Logout not working in Basic Auth in Spring Security

无人久伴 提交于 2019-12-24 22:54:03

问题


The official Spring Github Repo's Readme reads:

The application is almost finished functionally. The last thing we need to do is implement the logout feature that we sketched in the home page. If the user is authenticated then we show a "logout" link and hook it to a logout() function in the AppComponent. Remember, it sends an HTTP POST to "/logout" which we now need to implement on the server. This is straightforward because it is added for us already by Spring Security (i.e. we don’t need to do anything for this simple use case). For more control over the behaviour of logout you could use the HttpSecurity callbacks in your WebSecurityAdapter to, for instance execute some business logic after logout.

Taken from: https://github.com/spring-guides/tut-spring-security-and-angular-js/tree/master/single

However, I am using basic authentication and testing it with Postman app. The POST on '/logout' gives me a 403 Forbidden like so:

{
    "timestamp": "2018-07-30T07:42:48.172+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Forbidden",
    "path": "/logout"
}

My Security Configurations are:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                    .authorizeRequests()
                    .antMatchers("/user/save")
                    .permitAll()
                .and()
                    .authorizeRequests()
                    .antMatchers("/user/**")
                    .hasRole("USER")
                .and()
                    .authorizeRequests()
                    .antMatchers("/admin/**")
                    .hasRole("ADMIN")
                .and()
                    .httpBasic()
                .and()
                    .logout()
                    .permitAll()
                .and()
                    .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }

I want the session to be invalidated, all the cookies to be deleted, such that when I query again on the endpoint /user with wrong credentials, I should get a 403. However, even after POST on /logout (which gives 403 anyway), the application accepts the GET on /user from the previous session and shows me the details of the user.

The endpoint is:

@GetMapping
public Principal user(Principal user){
    return user;
}

来源:https://stackoverflow.com/questions/51590877/logout-not-working-in-basic-auth-in-spring-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!