Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使在使用setAuthenticated(false)后仍在spring security中获取主体对象_Java_Spring_Spring Security - Fatal编程技术网

Java 即使在使用setAuthenticated(false)后仍在spring security中获取主体对象

Java 即使在使用setAuthenticated(false)后仍在spring security中获取主体对象,java,spring,spring-security,Java,Spring,Spring Security,我正在使用UsernamePasswordAuthenticationToken对loggedin用户进行身份验证 但在那之后,在某些情况下,我想手动注销。在条件中,我已输入以下代码。我得到了authenticated=false,但主体对象仍然存在 SecurityContextHolder.getContext().getAuthentication() .setAuthenticated(false); SecurityContextHolder.clearContext();

我正在使用
UsernamePasswordAuthenticationToken
对loggedin用户进行身份验证

但在那之后,在某些情况下,我想手动注销。在条件中,我已输入以下代码。我得到了
authenticated=false
,但主体对象仍然存在

SecurityContextHolder.getContext().getAuthentication()
    .setAuthenticated(false);
SecurityContextHolder.clearContext();

SecurityContextHolder.getContext().setAuthentication(null)


authenticated
只是主体对象旁边的一个布尔标志。

我不知道您的Spring版本,但您仍然可以添加:

SecurityContextLogoutHandler logoutHandler = new SecurityContextLogoutHandler();

if( currentUser == null ) {
    logoutHandler.logout(request, response, auth);
}

调用clearContext()之后的下一步

感谢您的回复..经过身份验证后,我将使用UserInfo UserInfo=(UserInfo)auth.getPrincipal()获取当前用户;然后我设置setauthenticated(false)。iam getting authentication=false..但仍然能够使用UserInfo user=(UserInfo)auth.getPrincipal()找到当前用户的身份验证;在另一种方法中…所以我需要手动设置UserInfo user=null..正如你上面提到的..请回答我明白你的意思,但是你试过上面的代码吗?您能否确认,即使在此代码之后,您仍然能够获得用户信息?谢谢您的回复。但在代码中,您已经输入了if(currentUser=null)。但在我的情况下,当前用户在那里。我应该手动设置UserInfo==null,然后尝试您建议的这个方法。我想您可以尝试设置UserInfo==null。