package com.example.smsexam;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Random;
public class SMS {
public static void sendsms(String tmsg, String phone) throws NoSuchAlgorithmException, IOException {
String sms_url = "";
sms_url = "https://sslsms.cafe24.com/sms_sender.php"; // SMS 전송요청 URL
String user_id = "YOUR ID"; // SMS아이디
String secure = "YOUR KEY";//인증키
String msg = tmsg;
String rphone = phone;
String sphone1 = "YOUR"; // cafe24에 등록된 폰번호 010
String sphone2 = "PHONE"; // cafe24에 등록된 폰번호 1111
String sphone3 = "NUMBER";// cafe24에 등록된 폰번호 1111
String mode = "1";
String smsType = "5";
String[] host_info = sms_url.split("/");
String host = host_info[2];
String path = "/" + host_info[3];
int port = 80;
// 데이터 맵핑 변수 정의

String charsetType = "UTF-8";


String arrKey[]
= new String[] {"user_id","secure","msg", "rphone","sphone1","sphone2","sphone3"
,"mode","smsType"};
String valKey[]= new String[arrKey.length] ;
valKey[0] = user_id;
valKey[1] = secure;
valKey[2] = msg;
valKey[3] = rphone;
valKey[4] = sphone1;
valKey[5] = sphone2;
valKey[6] = sphone3;
valKey[7] = mode;
valKey[8] = smsType;

String boundary = "";
Random rnd = new Random();
String rndKey = Integer.toString(rnd.nextInt(32000));
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytData = rndKey.getBytes();
md.update(bytData);
byte[] digest = md.digest();

for(int i =0;i<digest.length;i++)
{
boundary = boundary + Integer.toHexString(digest[i] & 0xFF);
}
boundary = "---------------------"+boundary.substring(0,11);
// 본문 생성
String data = "";
String index = "";
String value = "";

for (int i=0;i<arrKey.length; i++)
{
index = arrKey[i];
value = valKey[i];
data +="--"+boundary+"\r\n";
data += "Content-Disposition: form-data; name=\""+index+"\"\r\n";
data += "\r\n"+value+"\r\n";
data +="--"+boundary+"\r\n";
}
//out.println(data);
InetAddress addr = InetAddress.getByName(host);
Socket socket = new Socket(host, port);
// 헤더 전송
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charsetType));
wr.write("POST "+path+" HTTP/1.0\r\n");
wr.write("Content-Length: "+data.length()+"\r\n");
wr.write("Content-type: multipart/form-data, boundary="+boundary+"\r\n");
wr.write("\r\n");
// 데이터 전송
wr.write(data);
wr.flush();
// 결과값 얻기
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),charsetType));
String line;
String alert = "";
ArrayList tmpArr = new ArrayList();
while ((line = rd.readLine()) != null) {
tmpArr.add(line);
}
wr.close();
rd.close();

}
}



Controller

package com.example.smsexam;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SmsController {

@GetMapping("/")
public String index() {
return "sms";
}

@PostMapping("/sms")
public String sms(@RequestParam ("phone") String phone,
@RequestParam ("message") String message
) throws NoSuchAlgorithmException, IOException {

SMS.sendsms(message, phone);

return "sms";
}

}



sms.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/sms" method="POST">
<p>내용 : <input type="text" name="message">
<p>번호 : <input type="text" name="phone">
<input type="submit" value="전송">
</form>
</body>
</html>



  



기본 응용


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

</head>
<body>
<br><br><br><br>

Member
<form action="" method="post">

<p>Phone Number : <input type="type" id="phonenum" placeholder="000-0000-0000">
<input type="button" onclick="phonenumjs();" value="Send"></p>


<p>Check Number : <input type="text" id="inputnum" placeholder="check number here...">
<input type="button" onclick="inputnumjs();" value="Check"></p>
<input id = "targetbtn" type="submit" value="Join" disabled></p>
</form>



<script>
var checknum;

function phonenumjs(){
let tnum = $("#phonenum").val();
alert(tnum);
$.ajax({
url:"/checknum",
type:"post",
dataType:"json",
data:{"phonenum" : tnum},
success: function(result){
alert("Send SMS");
checknum = result

}
});
}
function inputnumjs(){

let inputnum = $("#inputnum").val();
alert("from server " + checknum)

if(checknum == inputnum){
alert("OK");
let target = document.getElementById('targetbtn');
target.disabled = false;
}else{
alert("Try again...");
}
}
</script>
</body>
</html>




package com.example.smsexam;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SignupController {
@GetMapping("/signup")
public String signup() {
return "signup";
}

@PostMapping("/checknum")
public @ResponseBody String checknum(
@RequestParam ("phonenum") String phonenum
) throws NoSuchAlgorithmException, IOException {


Random rand = new Random();
String check = Integer.toString(rand.nextInt(9000) + 1000);

SMS.sendsms("확인 창에 " + check + "를 입력하세요", phonenum);

System.out.println(check);

return check;
}



}









한국정보시스템개발원 |
Hankook Information System Institute

austiny@snu.ac.kr / austiny@gatech.edu