Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 구글 로그인 기능 추가 #8

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'

// MySQL
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -30,6 +31,14 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

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

// jwt
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/GDGoC/BaS/config/JwtTokenProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.GDGoC.BaS.config;

import com.GDGoC.BaS.user.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class JwtTokenProvider {

@Value("${jwt.secret}")
private String jwtSecret;

@Value("${jwt.expiration}")
private long expiration;

public String generateToken(User user) {
Date now = new Date();
Date expiryDate = new Date(now.getTime() + expiration);

return Jwts.builder()
.setSubject(Long.toString(user.getUserId()))
.claim("email", user.getEmail())
.setIssuedAt(now)
.setExpiration(expiryDate)
.signWith(SignatureAlgorithm.HS512, jwtSecret)
.compact();
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/GDGoC/BaS/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import com.GDGoC.BaS.clothing.UserAccessory;
import com.GDGoC.BaS.clothing.UserHead;
import com.GDGoC.BaS.clothing.UserTowel;
import com.GDGoC.BaS.waterdrop.WaterdropHistory;
import com.GDGoC.BaS.notification.Notification;
import com.GDGoC.BaS.shower.UserRecord;
import com.GDGoC.BaS.user.enums.Eye;
import com.GDGoC.BaS.user.enums.Mouth;
import com.GDGoC.BaS.user.enums.Nose;
import com.GDGoC.BaS.user.enums.Provider;
import com.GDGoC.BaS.user.enums.Skin;
import com.GDGoC.BaS.waterdrop.WaterdropHistory;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
Expand All @@ -26,9 +27,11 @@
import jakarta.validation.constraints.Min;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
public class User {

Expand Down Expand Up @@ -67,6 +70,13 @@ public class User {
@Column(nullable = false, length = 20)
private Mouth mouth;

@Enumerated(STRING)
@Column(nullable = false)
private Provider provider;

@Column(nullable = false)
private String providerId;

@OneToMany(mappedBy = "user", cascade = ALL, orphanRemoval = true)
private List<WaterdropHistory> waterdropHistories = new ArrayList<>();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/GDGoC/BaS/user/enums/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.GDGoC.BaS.user.enums;

public enum Provider {
GOOGLE
}