开发者

Authenticate User manually

开发者 https://www.devze.com 2022-12-23 13:01 出处:网络
I am trying to authenticate the user after I got credentials using oAuth (with Twitter if that makes a difference). As far as I could understand it, I can directly put the Authentication object into S

I am trying to authenticate the user after I got credentials using oAuth (with Twitter if that makes a difference). As far as I could understand it, I can directly put the Authentication object into SecurityContextHolder. Here is how I do it:

Authentication auth = new TwitterOAuthAuthentication(member,
userDetailsService.loadUserByUsername(me开发者_运维百科mber.getUsername()).getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);

This for some reason does absolutely nothing. What am I missing and what should I do to accomplish what need?


Try adding

auth.setAuthenticated(true);

before you set the Authentication to the context.

The following code works fine for me (inside a filter class):

final Authentication auth = new CustomAuthenticationToken(user);
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号