Skip to content

Commit

Permalink
feat: CustomOauth2User (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Nov 28, 2023
1 parent 552e684 commit e8bf796
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

// oauth
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
}

tasks.named('bootBuildImage') {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/api/TaveShot/domain/Member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
Expand All @@ -19,4 +21,9 @@ public class Member extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long gitId;
private String gitLoginId;
private String gitEmail;
private String gitName;

}
37 changes: 37 additions & 0 deletions src/main/java/com/api/TaveShot/global/oauth2/CustomOauth2User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.api.TaveShot.global.oauth2;

import com.api.TaveShot.domain.Member.Member;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.user.OAuth2User;

@RequiredArgsConstructor
public class CustomOauth2User implements OAuth2User {

private final Member member;
private final Map<String, Object> attributes;

@Override
public <A> A getAttribute(String name) {
return OAuth2User.super.getAttribute(name);
}

@Override
public Map<String, Object> getAttributes() {
return attributes;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singleton(new SimpleGrantedAuthority("GITHUB_MEMBER"));
}

@Override
public String getName() {
return member.getGitName();
}
}

0 comments on commit e8bf796

Please sign in to comment.