- 0 . . Template
- 1 . Introduction.
- 1 1. . About Spring
- 1 2. . About Spring Boot
- 1 3. . Dev. Environment
- 1 4. . Hello World
- 1 5. . Summary
- 2 . Basic Database.
- 2 1. . MySql & Heidi
- 2 2. . JDBC
- 2 3. . MyBatis
- 2 4. . JPA
- 2 5. . Summary
- 3 . Basic Spring Security.
- 3 1. . About Spring Security
- 3 2. . Signup/Signin/Signout
- 3 3. . Security Information
- 3 4. . Fragment
- 3 5. . Summary
- 4 . Basic Project.
- 4 1. . Question & Answer
- 4 2. . Board & Reply
- 4 3. . Cart & Product
- 4 4. . Buying & Delivery
- 4 5. . Summary
- 5 . Advanced Project.
- 5 1. . Admin
- 5 2. . Chat with Ajax
- 5 3. . Other Login - Korean
- 5 4. . Like & View Count
- 5 5. . Summary
- 6 . Expert Project.
- 6 1. . Map - Korean
- 6 2. . Mail - Korean
- 6 3. . SMS - Korean
- 6 4. . Credit Card - Korean
- 6 5. . Summary
- 7 . Frontend Project.
- 7 1. . HTML/CSS/JS
- 7 2. . Bootstrap
- 7 3. . Theme
- 7 4. . Etc
- 7 5. . Summary
- 8 . Mini Project.
- 8 1. . Storyboard
- 8 2. . Backend - Korean
- 8 3. . Frontend
- 8 4. . Portfolio
- 8 5. . Summary
- 9 . Deploy. Project.
- 9 1. . AWS
- 9 2. . Gabia
- 9 3. . Domain
- 9 4. . Etc
- 9 5. . Summary
- A 1. Bug Repost.
- Appendix 0. .
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;
}
}
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