I'm using SpringBoot 3.2.2 and implementing social login using OAuth2. While writing CustomOAuth2UserService, an error occurred in the process of setting permissions. Below is the body of the code.
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
private final UserRepository userRepository;
private static final String NAVER="naver";
private static final String KAKAO="kakao";
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
log.info("CustomOAuth2UserService.loadUser() Run - Enter OAuth2 login request");
/*
* Create a DefaultOAuth2UserService object, create a DefaultOuth2User object through loadUser(userRequest), and return it
* The loadUser() of the DefaultOAuth2UserService sends a request to the user information provider URI of the social login API
* After obtaining user information, create a DefaultOAuth2User object through it and return it.
* As a result, the OAuth2User contains user information obtained from the OAuth service
*/
OAuth2UserService<OAuth2UserRequest, OAuth2User> delegate = new DefaultOAuth2UserService();
OAuth2User oAuth2User = delegate.loadUser(userRequest);
/*
* Extract registrationId from userRequest and save SocialType to registrationId
* * http://localhost:8080/oauth2/authorization/kakao에서 kakao가 registrationId
* The userNameAttributeName is later set to nameAttributeKey.
*/
String registrationId = userRequest.getClientRegistration().getRegistrationId();
SocialType socialType = getSocialType(registrationId);
String userNameAttributeName = userRequest.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
// The value that becomes the key (PK) at the time of OAuth2 login
Map<String, Object> attributes = oAuth2User.getAttributes(); // Json value of userInfo provided by API in social login (user information)
// Create OAuthAttributes objects through user information according to socialType
OAuthAttributes extractAttributes = OAuthAttributes.of(socialType, userNameAttributeName, attributes);
User createdUser = getUser(extractAttributes, socialType);
// Create and return Custom OAuth2User objects that implement DefaultOuth2User
return new CustomOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(createdUser.getRole().getKey())),
attributes,
extractAttributes.getNameAttributeKey(),
createdUser.getEmail(),
createdUser.getRole() //!!!!!error!!!!!
);
}
}
On the last line, Required type:javax.management.relation.Role Provided:com.Next.enums.Role error statement occurs.
In addition, User class block is below
package com.Next.domain;
import com.Next.enums.SocialType;
import com.Next.enums.Role;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.security.crypto.password.PasswordEncoder;
@Entity
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_id")
private Long id;
private String email;
private String password;
private String nickname;
@Enumerated(EnumType.STRING)
private Role role;
@Enumerated(EnumType.STRING)
private SocialType socialType; // KAKAO, NAVER, GOOGLE
private String socialId; // Identifier value of the logged-in social type
private String refreshToken;//refrsh token
//user authorize method: GUEST,USER.-->enumerated
//Initial login-> GUEST, after additional info->update to USER
public void authorizeUser() {
this.role = com.Next.enums.Role.USER;
}
//password Encoding method
public void passwordEncode(PasswordEncoder passwordEncoder){
this.password=passwordEncoder.encode(this.password);
}
public void updateRefreshToken(String updateRefreshToken){
this.refreshToken=updateRefreshToken;
}
}
I was expecting 'getRole()' import User's Role. I don't know why the user class email is called normally, but the role is not called. I'd appreciate it if you could help me!!