이번 시간에는 로그인 양식을 만들어 볼 것이다.
물론 이번 시간에 배운 양식을 그대로 계속 쓰지는 않을 것이고, 오늘 시간을 통해 어떻게 돌아가는지 이해한 뒤에는 Spring Security를 사용할 것이다.
잡설은 줄이고 시작해보자.
일단 @RequestParam과 ModelMap은 더는 필요하지 않다. 없애자. 그리고 login.jsp로 가서 form을 이용해 로그인 양식을 만들어보자.
```
<html>
<head>
<title>Login Page</title>
</head>
<body>
Welcome to the login page~!
<form>
Name: <input type="text" name="name">
Password: <input type="password" name="name">
<input type="submit">
</form>
</body>
</html>
```
이렇게 작성하고, 실행한 뒤, 양식에 값을 채우면 이전 시간에 url을 통해 값을 넘겼을 때처럼 url이 변하는 걸 확인할 수 있다.
그런데 이렇게 url을 통해 정보를 전송하는 건 안전하지 않다. 인터넷을 통해 접근한다고 할 때 과정 중에 있는 모든 라우터가 이 정보를 볼 수 있다.
이는 우리가 GET 메서드를 이용해 값을 전달하고 있기 때문이다. GET 메서드는 서버에서 어떤 데이터를 가져와서 보여줄 때 사용하기 때문에, 이런 경우에는 POST를 사용한다. POST는 body에 담겨져 전송되기 때문에 비교적 안전하지만 결국 이 또한 확인할 수 있기 때문에 암호화해서 전송하는 것이 좋다. 하지만 이번에는 암호화까진 안 하고 POST를 사용하는 걸로 하자. Get과 Post의 차이를 아시나요? (velog.io)
```
<html>
<head>
<title>Login Page</title>
</head>
<body>
Welcome to the login page~!
<form method="post">
Name: <input type="text" name="name">
Password: <input type="password" name="name">
<input type="submit">
</form>
</body>
</html>
```
하여 양식을 만들었다. 이제 값을 입력했을 때 데이터베이스에 저장되어 있는(혹은 허용된) 값이라면 정해진 페이지로 리디렉션 시키는 코드를 작성해보자.
도착하는 페이지의 이름은 welcome.jsp라고 하자. 별 내용은 없고, 그냥 진짜 환영하는 의미에서 환영!이라는 내용만 담자.
```
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
${name} Welcome~!
</body>
</html>
```
아까 말했지만, GET은 내용을 보여주기 위해 사용하고, POST는 무언가를 수정하는 등 행동을 위해 사용한다고 했다.
지금은 LoginController의 gotoLoginPage가 GET만을 이용하고 있다. 이를 두 가지로 나눠서, login.jsp를 보여주는 건 GET, 로딩된 login.jsp에서 로그인에 성공했을 때 welcome.jsp로 가게하는 건 POST로 작성하자.
```
@Service
public AuthenticationService {
public boolean authenticate(String username String password) {
boolean isValidUsername = username.equalsIgnoreCase("in28minutes");
boolean isValidPassword = password.equalsIgnoreCase("dummy");
return isValidUsername && isValidPassword
}
}
```
```
@Controller
public class LoginController {
AuthenticationService authenticationService;
public LoginController(AuthenticationService authenticationService) {
super();
this.authenticationService = authenticationService;
}
@RequestMapping(value="login", method=RequestMethod.GET)
public String gotoLoginPage() {
return "login";
}
@RequestMapping(value="login", method=RequestMethod.POST)
public String gotoWelcomePage(@RequestParam String name, @RequestParam String password, ModelMap model) {
if(authenticationService.authenticate(name, password)) {
model.put("name", name);
return "welcome";
}
model.put("errorMessage", "Invalid Credential. try again.");
return "login";
}
}
```
```
<html>
<head>
<title>Login Page</title>
</head>
<body>
Welcome to the login page~!
<pre>${errorMessage}</pre>
<form method="post">
Name: <input type="text" name="name">
Password: <input type="password" name="name">
<input type="submit">
</form>
</body>
</html>
```
'강의 > Java Spring Boot' 카테고리의 다른 글
세션, 모델, 그리고 요청 - @SessionAttributes (0) | 2025.01.26 |
---|---|
todolist 만들기 (0) | 2025.01.26 |
Spring MVC 작동원리 (0) | 2025.01.26 |
JSP 시작하기 (0) | 2025.01.26 |
Spring Boot 시작하기 (1) | 2025.01.26 |