1 | package com.tourMate.config.oauth2;
|
---|
2 |
|
---|
3 | import com.tourMate.dao.UsersDao;
|
---|
4 | import com.tourMate.entities.Role;
|
---|
5 | import com.tourMate.entities.User;
|
---|
6 | import org.springframework.security.core.AuthenticationException;
|
---|
7 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
8 | import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
|
---|
9 | import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
|
---|
10 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
---|
11 | import org.springframework.security.oauth2.core.user.OAuth2User;
|
---|
12 | import org.springframework.stereotype.Service;
|
---|
13 | import org.springframework.util.ObjectUtils;
|
---|
14 |
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.Objects;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class CustomOAuth2UserDetailService extends DefaultOAuth2UserService {
|
---|
20 |
|
---|
21 | private final UsersDao usersDao;
|
---|
22 |
|
---|
23 | public CustomOAuth2UserDetailService(UsersDao usersDao) {
|
---|
24 | this.usersDao = usersDao;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
|
---|
29 | OAuth2User oAuth2User = super.loadUser(userRequest);
|
---|
30 |
|
---|
31 | try
|
---|
32 | {
|
---|
33 | return checkOAuth2User(userRequest, oAuth2User);
|
---|
34 | }
|
---|
35 | catch (AuthenticationException e)
|
---|
36 | {
|
---|
37 | throw e;
|
---|
38 | }
|
---|
39 | catch (Exception ex)
|
---|
40 | {
|
---|
41 | throw ex;
|
---|
42 | //throw new InternalAuthenticationServiceException(ex.getMessage(), ex.getCause());
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private OAuth2User checkOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User)
|
---|
47 | {
|
---|
48 | OAuth2UserDetails oAuth2UserDetails = OAuth2UserDetailsFactory
|
---|
49 | .createOAuth2UserDetails(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes());
|
---|
50 |
|
---|
51 | if(ObjectUtils.isEmpty(oAuth2UserRequest))
|
---|
52 | {
|
---|
53 | throw new RuntimeException("Cannot identifty OAuth2 user!");
|
---|
54 | }
|
---|
55 |
|
---|
56 | User user = usersDao.findByUsernameAndProvider(
|
---|
57 | oAuth2UserDetails.getEmail(),
|
---|
58 | oAuth2UserRequest.getClientRegistration().getRegistrationId());
|
---|
59 | User userDetails = null;
|
---|
60 | if(user != null)
|
---|
61 | {
|
---|
62 | userDetails = user;
|
---|
63 | userDetails = updateOAuth2UserDetail(userDetails, oAuth2UserDetails);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | userDetails = registerOAuth2UserDetail(oAuth2UserRequest, oAuth2UserDetails);
|
---|
68 | }
|
---|
69 | return new OAuth2UserDetailsCustom(
|
---|
70 | userDetails.getUserID(),
|
---|
71 | userDetails.getUsername(),
|
---|
72 | userDetails.getPassword(),
|
---|
73 | Collections.singletonList(new SimpleGrantedAuthority(userDetails.getRole().getRoleName()))
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | public User registerOAuth2UserDetail(OAuth2UserRequest oAuth2UserRequest, OAuth2UserDetails oAuth2UserDetails)
|
---|
78 | {
|
---|
79 | Role r = usersDao.findById(1L);
|
---|
80 | User user = new User();
|
---|
81 | user.setName(Objects.requireNonNullElse(oAuth2UserDetails.getName(), ""));
|
---|
82 | user.setEmail(oAuth2UserDetails.getEmail());
|
---|
83 | user.setProvider(oAuth2UserRequest.getClientRegistration().getRegistrationId());
|
---|
84 | user.setRole(r);
|
---|
85 | return usersDao.updateUser(user);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public User updateOAuth2UserDetail(User user, OAuth2UserDetails oAuth2UserDetails)
|
---|
89 | {
|
---|
90 | user.setEmail(oAuth2UserDetails.getEmail());
|
---|
91 | return usersDao.mergeUser(user);
|
---|
92 | }
|
---|
93 | }
|
---|