โโSpring framework๋ฅผ ์ฌ์ฉํ์ฌ Database์ ์ฐ๋ํ์ฌ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ์์ฑํ๊ธฐโโ
M : User.java , UserDAO.java
V : loginForm.jsp
C : UserController.java
Service : UserService.java
๋จผ์ @SessionAttributes, @SessionAttribute ์ ์ฌ์ฉํ์ง ์๊ณ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ์์ฑํ๊ธฐ
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
String uid;
String pwd;
}
|
cs |
UserController.java
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
|
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ezen.spring.web.service.UserService;
import com.ezen.spring.web.vo.User;
@Controller
@RequestMapping("/login")
public class UserController
{
@Autowired
private UserService svc;
@GetMapping("/loginform")
public String loginForm()
{
return "/user/loginForm";
}
@PostMapping("/loginResult")
@ResponseBody
public Map<String,Boolean> loginResult(User user)
{
boolean ok = svc.login(user);
Map<String,Boolean> map = new HashMap<>();
map.put("login", ok);
return map;
}
}
|
cs |
UserService.java
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
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ezen.spring.web.dao.UserDAO;
import com.ezen.spring.web.vo.User;
import jakarta.servlet.http.HttpSession;
@Service
public class UserService
{
@Autowired
private UserDAO dao;
private HttpSession session;
@Autowired //setter injection
public void setSession(HttpSession session) {
this.session = session;
}
public boolean login(User user) {
boolean ok = dao.login(user);
if(ok) {
session.setAttribute("userid", user.getUid());
}
return ok;
}
}
|
cs |
UserDAO.jsp
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
53
54
55
56
57
58
59
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.stereotype.Component;
import com.ezen.spring.web.vo.User;
@Component
public class UserDAO {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private Connection getConn()
{
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "SCOTT", "TIGER");
this.conn = conn;
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean login(User user)
{
getConn();
try {
String sql = "SELECT * FROM emp2 WHERE ename=? AND empno=?";
this.pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getUid());
//.toUpperCase ์๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๋ ๋ฉ์๋
pstmt.setString(2, user.getPwd());
this.rs= pstmt.executeQuery();
return rs.next(); // next๋ก ๊ฐ๋ฅดํค๋ ํ์ด ์์ผ๋ฉด true
}
catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll();
}
return false;
}
private void closeAll()
{
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
|
cs |
loginForm.jsp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋ก๊ทธ์ธ ํผ</title>
<style type="text/css">
@import url(//fonts.googleapis.com/earlyaccess/jejumyeongjo.css);
.jm-font{
font-family: 'Jeju Myeongjo', cursive;
color: black;
font-size:3em;}
#id, #pw{
font-family: 'Jeju Myeongjo', serif;
color: black;
font-size:1.5em;}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;}
main {width:fit-content; margin:1em auto; }
main h3{ text-align: center;}
form {border:1px solid black; padding:1em;}
label {display:inline-block; width:3em; text-align: right; padding-right:1em; }
div.btn { width:fit-content; margin: 0.5em auto; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function getLogin()
{
var obj = $('#loginForm').serialize();
$.ajax({
url : '/login/loginResult',
method:'post',
data : obj,
cache:false,
dataType:'json',
success:function(res){
alert(res.login ? '๋ก๊ทธ์ธ ์ฑ๊ณต':'๋ก๊ทธ์ธ ์คํจ');
if(res.login){
}
},
error : function(xhr, status, err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
</head>
<body style="background-color: rgb(227,209,254);">
<div class="wrapper" >
<main class="main-font">
<h3 class="jm-font">๐ง๐ป๋ก๊ทธ์ธ</h3>
<form id="loginForm" onsubmit="return getLogin();">
<input type="hidden" name="cmd" value="adminlogin">
<div id ="id"><label for="uid">์์ด๋</label>
<input type="text" id="uid" name="uid" style="height:25px">
</div>
<p>
<div id="pw"><label for="pwd">์ํธ</label>
<input type="password" id="pwd" name="pwd" style="height:25px">
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">๋ก๊ทธ์ธ</button>
</div>
</form>
</main>
</div>
</body>
</html>
|
cs |
Spring์ ์๋ session Annotation์ ํ์ฉํ์ฌ ๋ก๊ทธ์ธ ํ๊ณ ์ฌ์ฉ์์ ์์ด๋๋ฅผ ์ธ์ ๊ฐ์ฒด์ ๋ด๊ธฐ
@SessionAttributes(" ์ธ์
์์ฑ์ด๋ฆ") - ํด๋์ค ์์๋ค๊ฐ ๋ถ์ธ๋ค. : ์ธ์
์ ์ ์ธํ๊ธฐ
@SessionAttribute("์ธ์
์์ฑ ์ด๋ฆ") - ๋ฉ์๋ ํ๋ผ๋ฏธํฐ : ์ธ์
์์ ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ
SessionStatus - ๋ฉ์๋ ํ๋ผ๋ฏธํฐ์
-status.setComplete();
์์
@Controller
@SessionAttributes("userid")
์์ฒญ ํ๋ผ๋ฏธํฐ userid์ ๊ฐ์ ์๋์ผ๋ก ์ธ์
์ ์ ์ฅ,
model.addAttribute("userid","smith")
๋ชจ๋ธ ๋ฐ์ดํฐ์ ํค๊ฐ ๊ฐ์ ๊ฒฝ์ฐ๋ ์ธ์
์ ๋ค์ด๊ฐ๋ค.
User.java
๋ณ์๋ช
์ ์์ ํ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
String userid;
String userpwd;
}
|
cs |
loginForm.jsp
์ ๋ณ์ name์ ํต์ผ ์์ผ์ฃผ๊ธฐ ์ํด ๊ณ ์ณค๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<form id="loginForm" onsubmit="return getLogin();">
<div id ="id"><label for="userid">์์ด๋</label>
<input type="text" id="userid" name="userid" style="height:25px">
</div>
<p>
<div id="pw"><label for="userpwd">์ํธ</label>
<input type="password" id="userpwd" name="userpwd" style="height:25px">
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">๋ก๊ทธ์ธ</button>
</div>
</form>
|
cs |
UserController.java
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
53
54
55
56
57
58
59
|
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.ezen.spring.web.service.UserService;
import com.ezen.spring.web.vo.User;
@Controller
@SessionAttributes("userid")
@RequestMapping("/login")
public class UserController
{
@Autowired
private UserService svc;
@GetMapping("/loginform")
public String loginForm()
{
return "/user/loginForm";
}
@PostMapping("/loginResult")
@ResponseBody
public Map<String,Boolean> loginResult(User user, Model m)
{
boolean ok = svc.login(user);
if(ok) {
m.addAttribute("userid", user.getUserid());
}
Map<String,Boolean> map = new HashMap<>();
map.put("login", ok);
return map;
}
@GetMapping("/logout")
@ResponseBody
public Map<String,Boolean> logout(SessionStatus status){
status.setComplete();
boolean logout = status.isComplete();
Map<String,Boolean> map = new HashMap<>();
map.put("logout", logout);
return map;
}
@GetMapping("/test")
@ResponseBody
public Map<String,String> isLogged(
@SessionAttribute(name="userid", required=false) String userid){
Map<String,String> map = new HashMap<>();
map.put("userid", userid);
return map;
}
}
|
cs |
์คํ ๊ฒฐ๊ณผ :
๋๊ธ