Skip to content

Commit

Permalink
Merge pull request #22 from YogitTeam/feat/user-login
Browse files Browse the repository at this point in the history
#21 feat: 유저 sms 인증 구현 완료
  • Loading branch information
shinhn authored Oct 22, 2022
2 parents 0376c3e + 82fea0e commit d085407
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 1 deletion.
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 server/src/main/java/com/yogit/server/sms/dto/MessageDTO.java
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 server/src/main/java/com/yogit/server/sms/dto/SmsNaverReq.java
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 server/src/main/java/com/yogit/server/sms/dto/SmsSendReq.java
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 server/src/main/java/com/yogit/server/sms/dto/SmsSendRes.java
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 server/src/main/java/com/yogit/server/sms/service/SmsService.java
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class User extends BaseEntity {

private Integer age;
private float memberTemp;
private String phone;

@Enumerated(EnumType.STRING)
private UserStatus userStatus;
Expand Down
8 changes: 7 additions & 1 deletion server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ logging:
com:
amazonaws:
util:
EC2MetadataUtils: ERROR
EC2MetadataUtils: ERROR

sms:
service-id: ncp:sms:kr:285557358037:yogit
access-key: mPcyRmD3xO51CI8d4DPE
secret-key: i1CzRXqgXVhIiRA9ojOLTWmICBJyzJRB6DbbmTV5
phone-num: 01046270562

0 comments on commit d085407

Please sign in to comment.