-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from YogitTeam/feat/user-login
#21 feat: 유저 sms 인증 구현 완료
- Loading branch information
Showing
8 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
server/src/main/java/com/yogit/server/sms/controller/SmsController.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,35 @@ | ||
package com.yogit.server.sms.controller; | ||
|
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.yogit.server.global.dto.ApplicationResponse; | ||
import com.yogit.server.sms.dto.SmsSendReq; | ||
import com.yogit.server.sms.dto.SmsSendRes; | ||
import com.yogit.server.sms.service.SmsService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URISyntaxException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Api(tags = "SMS 인증 API") | ||
public class SmsController { | ||
|
||
private final SmsService smsService; | ||
|
||
@ApiOperation(value = "문자발송", notes = "문자를 발송합니다.") | ||
@PostMapping("/sms") | ||
public ApplicationResponse<SmsSendRes> send(@RequestBody SmsSendReq smsSendReq) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, JsonProcessingException, URISyntaxException { | ||
return ApplicationResponse.ok(SmsSendRes.builder() | ||
.value(smsService.send(smsSendReq)) | ||
.build()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
server/src/main/java/com/yogit/server/sms/dto/MessageDTO.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,15 @@ | ||
package com.yogit.server.sms.dto; | ||
|
||
import io.swagger.annotations.ApiModel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ApiModel(description = "네이버에서 원하는 정보(메시지)를 담기 위한 객체") | ||
public class MessageDTO { | ||
private String to; | ||
private String content; | ||
} |
21 changes: 21 additions & 0 deletions
21
server/src/main/java/com/yogit/server/sms/dto/SmsNaverReq.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,21 @@ | ||
package com.yogit.server.sms.dto; | ||
|
||
import io.swagger.annotations.ApiModel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ApiModel(description = "네이버에서 원하는 정보를 담기 위한 객체") | ||
public class SmsNaverReq { | ||
private String type; | ||
private String from; | ||
private String Content; | ||
private List<MessageDTO> messages; | ||
} |
31 changes: 31 additions & 0 deletions
31
server/src/main/java/com/yogit/server/sms/dto/SmsSendReq.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,31 @@ | ||
package com.yogit.server.sms.dto; | ||
|
||
import com.yogit.server.user.entity.User; | ||
import io.swagger.annotations.ApiModel; | ||
import io.swagger.annotations.ApiModelProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Size; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ApiModel(description = "아이디 중복 확인을 위한 요청 객체") | ||
public class SmsSendReq { | ||
|
||
@NotBlank(message = "전화번호를 입력해 주세요.") | ||
@Size(min = 11, max = 13, message = "전화번호를 확인하여 주세요.") | ||
@ApiModelProperty(notes = "전화번호를 입력해 주세요.") | ||
private String phoneNum; | ||
|
||
public static SmsSendReq from(User user) { | ||
return SmsSendReq.builder() | ||
.phoneNum(user.getPhone()) | ||
.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
server/src/main/java/com/yogit/server/sms/dto/SmsSendRes.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,17 @@ | ||
package com.yogit.server.sms.dto; | ||
|
||
import io.swagger.annotations.ApiModel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ApiModel(description = "문자인증 값 반환을 위한 객체") | ||
public class SmsSendRes { | ||
private String value; | ||
private String message; | ||
} |
113 changes: 113 additions & 0 deletions
113
server/src/main/java/com/yogit/server/sms/service/SmsService.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,113 @@ | ||
package com.yogit.server.sms.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.yogit.server.sms.dto.MessageDTO; | ||
import com.yogit.server.sms.dto.SmsNaverReq; | ||
import com.yogit.server.sms.dto.SmsSendReq; | ||
import com.yogit.server.sms.dto.SmsSendRes; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.tomcat.util.codec.binary.Base64; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SmsService { | ||
|
||
@Value("${sms.service-id}") | ||
private String serviceId; | ||
|
||
@Value("${sms.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${sms.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("01046270562") | ||
private String phoneNum; | ||
|
||
public String send(SmsSendReq smsSendReq) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, JsonProcessingException, URISyntaxException { | ||
// if(smsSendReq.getPhoneNum().contains("-")) return "-을 제거해주세요."; | ||
|
||
System.out.println(serviceId); | ||
System.out.println(accessKey); | ||
System.out.println(secretKey); | ||
System.out.println(phoneNum); // TODO 위 3가지 변수와 다르게 @Value(application.yml)값이 다른 값으로 불러와지는 문제가 있음, 일단 값을 yml에서 가져오지 않고 바로 넣어주는 식으로 임시 해결 | ||
|
||
String certification = Integer.toString((int)(Math.random() * (99999 - 10000 + 1)) + 10000); | ||
|
||
Long time = System.currentTimeMillis(); | ||
|
||
List<MessageDTO> messages = new ArrayList<>(); | ||
messages.add(new MessageDTO(smsSendReq.getPhoneNum(), "Yogit \n 인증번호: "+certification)); | ||
|
||
SmsNaverReq smsNaverRequest = new SmsNaverReq("SMS", this.phoneNum, certification, messages); | ||
|
||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonBody = objectMapper.writeValueAsString(smsNaverRequest); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.set("x-ncp-apigw-timestamp", time.toString()); | ||
headers.set("x-ncp-iam-access-key", this.accessKey); | ||
String sig = makeSignature(time); // 암호화 | ||
headers.set("x-ncp-apigw-signature-v2", sig); | ||
|
||
HttpEntity<String> body = new HttpEntity<>(jsonBody, headers); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); | ||
SmsSendRes smsSendResponse = restTemplate.postForObject( | ||
new URI("https://sens.apigw.ntruss.com/sms/v2/services/" + this.serviceId + "/messages"), body, SmsSendRes.class); | ||
|
||
return certification; | ||
} | ||
|
||
public String makeSignature(Long time) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { | ||
|
||
String space = " "; | ||
String newLine = "\n"; | ||
String method = "POST"; | ||
String url = "/sms/v2/services/"+ this.serviceId+"/messages"; | ||
String timestamp = time.toString(); | ||
String accessKey = this.accessKey; | ||
String secretKey = this.secretKey; | ||
|
||
String message = new StringBuilder() | ||
.append(method) | ||
.append(space) | ||
.append(url) | ||
.append(newLine) | ||
.append(timestamp) | ||
.append(newLine) | ||
.append(accessKey) | ||
.toString(); | ||
|
||
SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256"); | ||
Mac mac = Mac.getInstance("HmacSHA256"); | ||
mac.init(signingKey); | ||
|
||
byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8")); | ||
String encodeBase64String = Base64.encodeBase64String(rawHmac); | ||
|
||
return encodeBase64String; | ||
} | ||
|
||
} |
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