在使用 oAuth 获得凭据后,我尝试对用户进行身份验证(如果有影响的话,可以使用 Twitter)。据我所知,我可以直接将 Authentication 对象放入 SecurityContextHolder 中。我是这样做的:
Authentication auth = new TwitterOAuthAuthentication(member,
userDetailsService.loadUserByUsername(member.getUsername()).getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
出于某种原因,这绝对没有任何作用。我缺少什么以及我应该做什么才能满足需求?
最佳答案
尝试添加
auth.setAuthenticated(true);
在将身份验证设置为上下文之前。
以下代码对我来说效果很好(在过滤器类中):
final Authentication auth = new CustomAuthenticationToken(user);
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
关于spring-security - 手动验证用户身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2466312/