#16 board: kakao oauth settings

This commit is contained in:
haerong22
2023-01-25 01:14:22 +09:00
parent 11d160b3fa
commit 9bec8a1f77
5 changed files with 50 additions and 11 deletions

View File

@@ -23,18 +23,18 @@ public abstract class AuditingFields {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt; // 생성일시
protected LocalDateTime createdAt; // 생성일시
@CreatedBy
@Column(nullable = false, updatable = false, length = 100)
private String createdBy; // 생성자
protected String createdBy; // 생성자
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@LastModifiedDate
@Column(nullable = false)
private LocalDateTime modifiedAt; // 수정일시
protected LocalDateTime modifiedAt; // 수정일시
@LastModifiedBy
@Column(nullable = false, length = 100)
private String modifiedBy; //수정자
protected String modifiedBy; //수정자
}

View File

@@ -8,9 +8,8 @@ import javax.persistence.*;
import java.util.Objects;
@Getter
@ToString
@ToString(callSuper = true)
@Table(indexes = {
@Index(columnList = "userId", unique = true),
@Index(columnList = "email", unique = true),
@Index(columnList = "createdAt"),
@Index(columnList = "createdBy")
@@ -38,16 +37,22 @@ public class UserAccount extends AuditingFields {
protected UserAccount() {}
private UserAccount(String userId, String userPassword, String email, String nickname, String memo) {
private UserAccount(String userId, String userPassword, String email, String nickname, String memo, String createdBy) {
this.userId = userId;
this.userPassword = userPassword;
this.email = email;
this.nickname = nickname;
this.memo = memo;
this.createdBy = createdBy;
this.modifiedAt = createdAt;
}
public static UserAccount of(String userId, String userPassword, String email, String nickname, String memo) {
return new UserAccount(userId, userPassword, email, nickname, memo);
return new UserAccount(userId, userPassword, email, nickname, memo, null);
}
public static UserAccount of(String userId, String userPassword, String email, String nickname, String memo, String createdBy) {
return new UserAccount(userId, userPassword, email, nickname, memo, createdBy);
}
@Override

View File

@@ -5,8 +5,10 @@ import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.user.OAuth2User;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -16,10 +18,16 @@ public record BoardPrincipal(
Collection<? extends GrantedAuthority> authorities,
String email,
String nickname,
String memo
) implements UserDetails {
String memo,
Map<String, Object> oAuth2Attributes
) implements UserDetails, OAuth2User {
public static BoardPrincipal of(String username, String password, String email, String nickname, String memo) {
return of(username, password, email, nickname, memo, Map.of());
}
public static BoardPrincipal of(String username, String password, String email, String nickname, String memo, Map<String, Object> oAuth2Attributes) {
Set<RoleType> roleTypes = Set.of(RoleType.USER);
@@ -32,7 +40,8 @@ public record BoardPrincipal(
.collect(Collectors.toUnmodifiableSet()),
email,
nickname,
memo
memo,
oAuth2Attributes
);
}
@@ -76,6 +85,12 @@ public record BoardPrincipal(
@Override public boolean isCredentialsNonExpired() { return true; }
@Override public boolean isEnabled() { return true; }
@Override
public Map<String, Object> getAttributes() { return oAuth2Attributes; }
@Override
public String getName() { return username; }
public enum RoleType {
USER("ROLE_USER");

View File

@@ -32,6 +32,23 @@ spring:
thymeleaf3:
decoupled-logic: true
security:
oauth2:
client:
registration:
kakao:
client-id: ${KAKAO_OAUTH_CLIENT_ID}
client-secret: ${KAKAO_OAUTH_CLIENT_SECRET}
authorization-grant-type: authorization_code
redirect-uri: "{baseUrl}/login/oauth2/code/kakao"
client-authentication-method: POST
provider:
kakao:
authorization-uri: https://kauth.kakao.com/oauth/authorize
token-uri: https://kauth.kakao.com/oauth/token
user-info-uri: https://kapi.kakao.com/v2/user/me
user-name-attribute: id
---
spring: