로그인

2024. 6. 25. 18:08JSP+Spring Boot

HttpSession session
사용자와 서버 간의 상태를 유지하는데 사용되는 객체
- 상태를 유지, 식별 및 데이터 저장, 수명을 관리
로그인을 하는 상태를 유지
- 데이터를 전달받은 값이 일치하는지 식별
- 일정한 기간동안 데이터를 저장하고 일정 시간이 지나면 수명이 다하도록 수명을 관리할 수 있음(로그인 시간유지)

 

로그인의 경우 ParameterType에 Map을 입력해야한다.

 -> where에서 가져와야하는 값이 여러개일 경우 Map
parameterType="Map"
parameterType="java.util.Map"
Map = key, value
각각의 자료형에 맞는 값을 가지고 와서 넣어준다는 의미

 

member-mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
<mapper namespace="com.khmall.mapper.MemberMapper">
	<!-- Login을 통해 멤버 정보 가져오기 -->
	
	<select id="getLogin" resultType="com.khmall.dto.Member" parameterType="java.util.Map">
		select * from member
		where member_name = #{member_name} <!-- 사용자가 입력하는 이름값이 들어올 수 있도록 설정 -->
		and member_phone = #{member_phone} <!-- 사용자가 입력한 번호값이 들어올 수 있도록 설정 -->
		<!-- 위 두 개가 모두 일치하는 사람이 있다면 로그인 -->
	</select>
</mapper>

 

-------------------------------------------------------------------------------------------------------------------------------

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>메인 페이지</title>
</head>
<body>
	<div th:if="${session.loginSession != null}">
		<p th:text="${session.loginSession.member_name}+'님, 환영합니다.'"></p>
		<a href="/logout">로그아웃</a>
	</div>
	<div th:if="${session.loginSession == null}">
		<p>로그인하지 않았습니다.</p>
		<a href="">회원가입하러가기</a>
		<a href="/login">로그인하기</a>
	</div>
</body>
</html> 

 

-------------------------------------------------- -------------------------------------------------- --------------------------------------------------

 

login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>로그인</title>
</head>
<body>
	<h1>로그인</h1>
	<form action="/login" method="post">
	<!-- nickname 대신 name을 이용, password 대신 phone을 이용 -->
		<input type="text" th:field="${m.member_name}"><br>
		<input type="text" th:field="${m.member_phone}"><br>
		<button>로그인</button>
	</form>
	<div th:if="${error}">
		<p th:text="${error}" style="color: red;"></p>
	</div>
</body>
</html>

 

-------------------------------------------------- -------------------------------------------------- --------------------------------------------------

 

Mapper

package com.khmall.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.khmall.dto.Member;

@Mapper
public interface MemberMapper {
	/*
	Member getLogin(@Param("member_name") String member_name,
					@Param("member_phone") String member_phone);
					
	@Param : 앞에서 매개변수인 파라미터를 이용해서 일치하는 값을 가져온다고 한 번 더 확실히 작성하는 것
	
	*/
	Member getLogin(@Param("member_name") String member_name,
					@Param("member_phone") String member_phone);
	
}

 

-------------------------------------------------- -------------------------------------------------- --------------------------------------------------

 

Service

 

package com.khmall.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.khmall.dto.Member;
import com.khmall.mapper.MemberMapper;

@Service
public class MemberService {

	@Autowired
	private MemberMapper memberMapper;
	
	public Member getLogin(String member_name, String member_phone) {
		//추후에 서비스에 비밀번호를 암호화해서 db에 저장하고
		//암호화된 비밀번호를 가져와서 로그인하는 코드 작성
		
		return memberMapper.getLogin(member_name, member_phone);
		
	}
}

 

 

-------------------------------------------------- -------------------------------------------------- --------------------------------------------------

 

dto

 

package com.khmall.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Member {
	
	private int member_id;
	private String member_name;
	private int member_age;
	private String member_phone;
}

 

-------------------------------------------------- -------------------------------------------------- --------------------------------------------------

 

Controller

 

package com.khmall.controller;

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.RequestParam;

import com.khmall.dto.Member;
import com.khmall.service.MemberService;

import jakarta.servlet.http.HttpSession;

@Controller
public class MemberController {

	@Autowired
	private MemberService memberService;
	
	@GetMapping("/login")
	public String showLoginForm(Model model) {
		model.addAttribute("m", new Member());
		
		return "login";
	}
	@PostMapping("login")
	public String getLogin(
			Model model,
			@RequestParam("member_name") String member_name,
			@RequestParam("member_phone") String member_phone,
			HttpSession session
			) {
		Member member = memberService.getLogin(member_name, member_phone);
		if(member != null) {//member 객체에 값이 들어있다면
			session.setAttribute("loginSession", member);
			return "redirect:/"; // 로그인 성공시 메인 페이지로 이동
		}
		else {//일치하는 정보가 없다면
			model.addAttribute("error", "일치하는 아이디, 비밀번호가 없습니다.");
			model.addAttribute("m", new Member());
			return "login";
		}
	}
	
	@GetMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();//invalidate : 무효시킴
		
		return "redirect:/";
	}

}

 

-----------------------------------------------------------------------------------------------------------------------------------

 

로그인 실패

 

로그인 성공