본문 바로가기

SpringBoot

Spring Boot + CoolSMS

1. CoolSMS란?

  • 카카오 알림톡 및 문자메시지 웹 발송을 지원해주는 REST API 제공 서비스이다.
  • 단문, 장문, 포토 메시지 등을 보낼 수 있다.

 

2. CoolSMS 회원가입 및 API KEY 생성

 

세상에서 가장 안정적이고 빠른 메시지 발송 플랫폼 - 쿨에스엠에스

손쉬운 결제 전용계좌, 신용카드, 계좌이체 등 국내 결제 뿐만 아니라 해용신용카드로 한번의 카드번호 등록으로 자동충전까지 지원합니다. 전용계좌, 신용카드, 계좌이체 등 다양한 결제 방식

coolsms.co.kr

 

3. CoolSMS Library 다운받기

1) 메인페이지에서 개발자센터 창을 클릭한다.

2) SDK 부분에서 JAVA를 클릭한다. 

 

3) STEP2 에서 GIT에 접속하고 zip 파일을 다운 받는다.

4) Zip 파일을 압축하면 lib 폴더 안에 jar 파일 두 개가 들어있을 것이다. 

  • jar 파일 두 개를 해당 프로젝트에 추가 해주면 된다.
    (eclipse 인 경우, 프로젝트 우클릭 → Build Path → configure Build Path
    Libraries 탭 클릭 → 우측의 Add JARs 버튼 클릭 후 해당 jar 파일 두 개 지정)

 

4. Spring Boot 기본 설정 및 코드 작성하기

1) build.gradle 파일에 의존성을 추가해준다.

compile group: 'net.nurigo', name: 'javaSDK', version: '2.2'

2) Controller

// coolSMS 테스트 화면
@GetMapping("/sms")
public String mySms() {
	return "order/sms";
}
    
// coolSMS 구현 로직 연결  
@GetMapping("/check/sendSMS")
public @ResponseBody String sendSMS(@RequestParam(value="to") String to) throws CoolsmsException {  	
	return boardService.PhoneNumberCheck(to);
}

3) Service 로직

public String PhoneNumberCheck(String to) throws CoolsmsException {
		
		String api_key = "NCSHONZYXFYSSMEB";
		String api_secret = "UNNGPMH4FUXIRCPQY2VSJPZFOZTB80QE";
		Message coolsms = new Message(api_key, api_secret);
		
		Random rand  = new Random();
    String numStr = "";
    for(int i=0; i<4; i++) {
       String ran = Integer.toString(rand.nextInt(10));
       numStr+=ran;
    }          

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("to", to);    // 수신전화번호 (ajax로 view 화면에서 받아온 값으로 넘김)
    params.put("from", "01072791324");    // 발신전화번호. 테스트시에는 발신,수신 둘다 본인 번호로 하면 됨
    params.put("type", "sms"); 
    params.put("text", "인증번호는 [" + numStr + "] 입니다.");

    coolsms.send(params); // 메시지 전송
        
    return numStr;

}

 

4) View 화면

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> 
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<title>문자인증</title>
</head>
<body>
	<div id="contents"> 
		받는사람 : <input type="text" id="to" name="to"/>   <!-- 인증번호 받을사람 휴대폰 번호 -->
		<button type="button" id="send">전송</button><br> <!-- 문자보내는 전송버튼 -->
		
		인증번호 : <input type="text" id="userNum">   <!-- 인증번호 입력창 -->
		<button type="button" id="enterBtn">확인</button>  <!-- 인증번호와 내가 입력창에 입력한 인증번호 비교하는 창 -->		
    </div>
</body>

<script type="text/javascript">
$('#send').click(function() {
	
	const to = $('#to').val();
	
	$.ajax ({
		url: '/check/sendSMS',
		type: 'GET',
		data: {
			"to" : to
		},
		success: function(data) {
			const checkNum = data;
			alert('checkNum:'+ checkNum);
			
			$('#enterBtn').click(function() {	
				const userNum = $('#userNum').val();
				
				if(checkNum === userNum) {
					alert('인증 성공하였습니다.');
				}
				else {
					alert('인증 실패하였습니다. 다시 입력해주세요.');
				}
			});
			
		}
	});
	
});
</script>

</html>
SMALL