Answer the question
In order to leave comments, you need to log in
Spring Security: how to authorize a user without a password?
Hello,
Help me understand how to make automatic login through oauth2.
I send a request to vk.com
String url = "https://api.vk.com/method/users.get"
+ "?uids={user_id}"
+ "&access_token={access_token}"
+ "&fields=uid,first_name,last_name,screen_name,sex,bdate,photo_100,photo_max_orig";
Map<String, List> result = getForObject(url, Map.class, userId, accessToken);
{uid=12345, first_name=xxx, last_name=xxx, sex=2, screen_name=xxx, bdate=1.5, photo_100=xxx, photo_max_orig=xxx}
@Autowired
private UserService userService;
....
UserDetails userDetails = userService.loadUserByOauth2("vk", 12345);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));
public class UserService implements UserDetailsService {
public UserDetails loadUserByOauth2(String provider, int clientId) throws UsernameNotFoundException {
UserDomain userDomain = userMapper.getUserByOauth2(clientId);
GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + userDomain.getRole());
return new User(userDomain.getEmail(), userDomain.getPassword(), Arrays.asList(authority));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question