-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] oauth2를 이용한 로그인 구현
- Loading branch information
Showing
26 changed files
with
544 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/leets/xcellentbe/global/auth/email/AuthCodeAlreadySentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import com.leets.xcellentbe.global.error.ErrorCode; | ||
import com.leets.xcellentbe.global.error.exception.CommonException; | ||
|
||
public class AuthCodeAlreadySentException extends CommonException { | ||
public AuthCodeAlreadySentException() { | ||
super(ErrorCode.AUTH_CODE_ALREADY_SENT); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/leets/xcellentbe/global/auth/email/EmailCannotBeSent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import com.leets.xcellentbe.global.error.ErrorCode; | ||
import com.leets.xcellentbe.global.error.exception.CommonException; | ||
|
||
public class EmailCannotBeSent extends CommonException | ||
{ | ||
public EmailCannotBeSent() | ||
{ | ||
super(ErrorCode.EMAIL_CANNOT_BE_SENT); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/leets/xcellentbe/global/auth/email/EmailCheckDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class EmailCheckDto { | ||
private String email; | ||
private String authNum; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/leets/xcellentbe/global/auth/email/EmailRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class EmailRequestDto { | ||
private String email; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/leets/xcellentbe/global/auth/email/EmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import org.springframework.stereotype.Service; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import java.util.Random; | ||
|
||
import com.leets.xcellentbe.global.error.exception.custom.InvalidInputValueException; | ||
|
||
@Transactional | ||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class EmailService { | ||
private final RedisService redisService; | ||
private final JavaMailSender mailSender; | ||
private int authNumber; | ||
|
||
//임의의 6자리 양수를 반환합니다. | ||
public void makeRandomNumber() { | ||
Random r = new Random(); | ||
this.authNumber = 100000 + r.nextInt(900000); | ||
} | ||
|
||
|
||
//mail을 어디서 보내는지, 어디로 보내는지 , 인증 번호를 html 형식으로 어떻게 보내는지 작성합니다. | ||
public String joinEmail(String email) throws MessagingException { | ||
makeRandomNumber(); | ||
String toMail = email; | ||
String title = "회원 가입 인증 이메일 입니다."; // 이메일 제목 | ||
String content = | ||
"인증 번호는 " + authNumber + "입니다." + | ||
"<br>" + | ||
"인증번호를 제대로 입력해주세요"; //이메일 내용 삽입 | ||
mailSend(toMail, title, content); | ||
return Integer.toString(authNumber); | ||
} | ||
|
||
//이메일을 전송합니다. | ||
public void mailSend(String toMail, String title, String content) throws MessagingException { | ||
|
||
if(redisService.getData(toMail)!=null){ | ||
throw new AuthCodeAlreadySentException(); | ||
} | ||
|
||
try { | ||
MimeMessage message = mailSender.createMimeMessage();//JavaMailSender 객체를 사용하여 MimeMessage 객체를 생성 | ||
MimeMessageHelper helper = new MimeMessageHelper(message, true, | ||
"utf-8");// true를 전달하여 multipart 형식의 메시지를 지원하고, "utf-8"을 전달하여 문자 인코딩을 설정 | ||
helper.setTo("h");//이메일의 수신자 주소 설정 | ||
helper.setSubject(title);//이메일의 제목을 설정 | ||
helper.setText(content, true);//이메일의 내용 설정 두 번째 매개 변수에 true를 설정하여 html 설정으로한다. | ||
mailSender.send(message); | ||
} | ||
catch(Exception e) { | ||
throw new EmailCannotBeSent(); | ||
} | ||
redisService.setDataExpire(toMail,Integer.toString(authNumber)); | ||
} | ||
|
||
public String checkAuthNum(String email, String authNum) { | ||
String storedAuthNum = redisService.getData(email); | ||
|
||
if (storedAuthNum == null) { | ||
return "인증번호가 만료되었습니다."; | ||
} | ||
else if(!storedAuthNum.equals(authNum)){ | ||
throw new InvalidInputValueException(); | ||
} | ||
else { | ||
return "인증에 성공하였습니다."; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/leets/xcellentbe/global/auth/email/RedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.leets.xcellentbe.global.auth.email; | ||
|
||
import java.time.Duration; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisService { | ||
private static final long AUTH_CODE_EXPIRE_SECONDS = 300; | ||
|
||
private final StringRedisTemplate redisTemplate;//Redis에 접근하기 위한 Spring의 Redis 템플릿 클래스 | ||
|
||
public String getData(String key){//지정된 키(key)에 해당하는 데이터를 Redis에서 가져오는 메서드 | ||
ValueOperations<String,String> valueOperations=redisTemplate.opsForValue(); | ||
return valueOperations.get(key); | ||
} | ||
public void setData(String key,String value){//지정된 키(key)에 값을 저장하는 메서드 | ||
ValueOperations<String,String> valueOperations=redisTemplate.opsForValue(); | ||
valueOperations.set(key,value); | ||
} | ||
public void setDataExpire(String key,String value){//지정된 키(key)에 값을 저장하고, 지정된 시간(duration) 후에 데이터가 만료되도록 설정하는 메서드 | ||
ValueOperations<String,String> valueOperations=redisTemplate.opsForValue(); | ||
Duration expireDuration=Duration.ofSeconds(AUTH_CODE_EXPIRE_SECONDS); | ||
valueOperations.set(key,value,expireDuration); | ||
} | ||
public void deleteData(String key){//지정된 키(key)에 해당하는 데이터를 Redis에서 삭제하는 메서드 | ||
redisTemplate.delete(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/leets/xcellentbe/global/auth/login/oauth/CustomOAuthUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.leets.xcellentbe.global.auth.login.oauth; | ||
|
||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import com.leets.xcellentbe.domain.user.Role; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* DefaultOAuth2User를 상속하고, email과 role 필드를 추가로 가진다. | ||
*/ | ||
@Getter | ||
public class CustomOAuthUser extends DefaultOAuth2User { | ||
|
||
private String email; | ||
private Role role; | ||
|
||
public CustomOAuthUser(Collection<? extends GrantedAuthority> authorities, | ||
Map<String, Object> attributes, String nameAttributeKey, | ||
String email, Role role) { | ||
super(authorities, attributes, nameAttributeKey); | ||
this.email = email; | ||
this.role = role; | ||
} | ||
} |
Oops, something went wrong.