|
| 1 | +package team.streetalk.service; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import org.apache.tomcat.util.codec.binary.Base64; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.http.HttpEntity; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | +import org.springframework.transaction.annotation.Transactional; |
| 15 | +import org.springframework.web.client.RestTemplate; |
| 16 | +import team.streetalk.dto.sms.MessagesRequestDto; |
| 17 | +import team.streetalk.dto.sms.SmsRequest; |
| 18 | +import team.streetalk.dto.sms.SmsResponse; |
| 19 | + |
| 20 | +import javax.crypto.Mac; |
| 21 | +import javax.crypto.spec.SecretKeySpec; |
| 22 | +import java.io.UnsupportedEncodingException; |
| 23 | +import java.net.URI; |
| 24 | +import java.net.URISyntaxException; |
| 25 | +import java.security.InvalidKeyException; |
| 26 | +import java.security.NoSuchAlgorithmException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +@Service |
| 31 | +@Transactional |
| 32 | +public class SmsService { |
| 33 | + |
| 34 | + private String serviceId = "ncp:sms:kr:273379428439:streetalk"; |
| 35 | + private String accessKey = "dQZPT2Sgg1jfNuPInrIR"; |
| 36 | + private String secretKey = "2gi7xUYug4XGrAd9jwRVO1yi68LiZwNMc3hLPImv"; |
| 37 | + |
| 38 | + |
| 39 | + public SmsResponse sendSms(String recipientPhoneNumber, String content) throws JsonProcessingException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, URISyntaxException { |
| 40 | + Long time = System.currentTimeMillis(); |
| 41 | + List<MessagesRequestDto> messages = new ArrayList<>(); |
| 42 | + messages.add(new MessagesRequestDto(recipientPhoneNumber, content)); |
| 43 | + SmsRequest smsRequest = new SmsRequest("SMS", "COMM", "82", "01030323682", "MangoLtd", messages); |
| 44 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 45 | + String jsonBody = objectMapper.writeValueAsString(smsRequest); |
| 46 | + HttpHeaders headers = new HttpHeaders(); |
| 47 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 48 | + headers.set("x-ncp-apigw-timestamp", time.toString()); |
| 49 | + headers.set("x-ncp-iam-access-key", this.accessKey); |
| 50 | + String sig = makeSignature(time); |
| 51 | + headers.set("x-ncp-apigw-signature-v2", sig); |
| 52 | + HttpEntity<String> body = new HttpEntity<>(jsonBody,headers); |
| 53 | + RestTemplate restTemplate = new RestTemplate(); |
| 54 | + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); |
| 55 | + SmsResponse smsResponse = restTemplate.postForObject(new URI("https://sens.apigw.ntruss.com/sms/v2/services/"+this.serviceId+"/messages"), body, SmsResponse.class); |
| 56 | + return smsResponse; |
| 57 | + |
| 58 | + } |
| 59 | + public String makeSignature(Long time) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { |
| 60 | + |
| 61 | + String space = " "; // one space |
| 62 | + String newLine = "\n"; // new line |
| 63 | + String method = "POST"; // method |
| 64 | + String url = "/sms/v2/services/"+ this.serviceId+"/messages"; // url (include query string) |
| 65 | + String timestamp = time.toString(); // current timestamp (epoch) |
| 66 | + String accessKey = this.accessKey; // access key id (from portal or Sub Account) |
| 67 | + String secretKey = this.secretKey; |
| 68 | + |
| 69 | + String message = new StringBuilder() |
| 70 | + .append(method) |
| 71 | + .append(space) |
| 72 | + .append(url) |
| 73 | + .append(newLine) |
| 74 | + .append(timestamp) |
| 75 | + .append(newLine) |
| 76 | + .append(accessKey) |
| 77 | + .toString(); |
| 78 | + |
| 79 | + SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256"); |
| 80 | + Mac mac = Mac.getInstance("HmacSHA256"); |
| 81 | + mac.init(signingKey); |
| 82 | + |
| 83 | + byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8")); |
| 84 | + String encodeBase64String = Base64.encodeBase64String(rawHmac); |
| 85 | + |
| 86 | + return encodeBase64String; |
| 87 | + } |
| 88 | +} |
0 commit comments