为什么 SpringSecurity 在注销后继续提供相同的经过身份验证的委托人

Why SpringSecurity, after a logout, keeps giving the same authenticated Principal

所以我使用 Spring 安全和 Spring 引导。我想制作我自己的 AuthenticationProvider,它以我自己的方式使用数据库,所以我用这个 authenticate 方法做到了:

@Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String email = authentication.getName();
        String password = authentication.getCredentials().toString();


        UserWithEmail userWithEmail = authService.getUserByEmail(email);
        if (userWithEmail == null)
            return null;
        if (userWithEmail.getPassword().equals(password)) {



            UsernamePasswordAuthenticationToken authenticated_user = new UsernamePasswordAuthenticationToken(userWithEmail, password, Arrays.asList(REGISTERED_USER_SIMPLE_GRANTED_AUTHORITY));
            return authenticated_user;
        } else {
            return null;
        }
    }

如果我将默认的 /login 页面与表单一起使用,效果很好,之后如果我将以下 ModelAttribute 添加到 Controller,它会正确填充 UserWithEmail对象:

@ModelAttribute("userwithemail")
    public UserWithEmail userWithEmail(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        Object principal = authentication.getPrincipal();
        if (principal instanceof UserWithEmail)
            return (UserWithEmail) principal;
        else
            return null;

    }

问题是,如果我点击 /login?logout,它会正确显示我已注销,但如果我再次通过控制器,我仍然会得到与主体相同的 UserWithEmail 对象,并且它有属性authenticated=true

这是我的 Java spring 安全配置:

http
                .formLogin()
                .defaultSuccessUrl( "/" )
                .usernameParameter( "username" )
                .passwordParameter( "password" )
                .and()

                .logout().invalidateHttpSession(true).deleteCookies("JSESSIONID").permitAll().and()

                .authorizeRequests()
                .antMatchers("*/**").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers("/profile").hasRole(MyAuthenticationProvider.REGISTERED_USER_AUTH)

                .and().authenticationProvider(getAuthProvider());

我是 Spring 安全方面的新手,所以我可能遗漏了一些东西...有人可以帮忙吗?

根据文档 here for CSRF POST 是注销所必需的,连同用于攻击保护的 CSRF 令牌。

因为我使用的是自定义模板引擎,所以我必须从请求中拦截模型属性中的 CSRF 令牌,如下所示:

@ModelAttribute("crsf_token")
public CsrfToken getcrsfToken(HttpServletRequest request, Model model) {
    CsrfToken token = (CsrfToken) request.getAttribute("_csrf");

    return token;
}

因为它没有被复制到我的模板引擎的模型中。