2021-03-31-TIL
2021-03-31-TIL
에러 코드로 예외 분기하기
ajax로 카운트하기 서버에서 값만 가져오는 방식으로
url 디코딩 안하면 에러남
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package honux.spring.playground.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
@RestController
@RequestMapping("/login")
public class UserController {
//sessionid, userid;
private Map<String,String> session = new HashMap<>();
@GetMapping
public MyResponse welcome() {
return new MyResponse("ok", "Kite");
}
@GetMapping("/check")
public MyResponse check(HttpServletRequest req) {
String key = "sid";
Cookie[] cookies = req.getCookies();
String uid = "Null";
for(Cookie c: cookies) {
if (c.getName().equals(key)) {
uid = session.getOrDefault(c.getValue(), "Null");
System.out.println("check: " + c.getValue() + ": " + uid);
}
}
return new MyResponse("ok", "login id: " + uid);
}
@PostMapping
public MyResponse login(String id, HttpServletResponse response) {
System.out.println(id);
Random r = new Random();
String sid = Integer.toString(r.nextInt());
session.put(sid, id);
System.out.println("login: " + sid+ ": " + session.get(sid));
response.addCookie(new Cookie("sid", sid));
return new MyResponse("ok","sid: " + sid);
}
}
This post is licensed under CC BY 4.0 by the author.