240220 스프링(로그인, 로그아웃)
2024. 2. 20. 17:13ㆍ2023.11.21-2024.05.31
메뉴에 로그인 있음
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메뉴</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark" id="mainNav">
<div class="container">
<a class="navbar-brand" href="./index"><img
src="assets/apple-icon-152x152.png" alt="..." /></a>
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse" data-bs-target="#navbarResponsive"
aria-controls="navbarResponsive" aria-expanded="false"
aria-label="Toggle navigation">
Menu
<i class="fas fa-bars ms-1"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
<li class="nav-item"><a class="nav-link" href="./board">게시판</a></li>
<li class="nav-item"><a class="nav-link" href="./board">공지</a></li>
<li class="nav-item"><a class="nav-link" href="./board">로그인</a></li>
</ul>
</div>
</div>
</nav>
</body>
</html>
LoginController.java
로그인 페이지 열어주기
package org.mask.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
//2024.02.20 get/login
@GetMapping("/login")//로그인 페이지 열어주는 일만 할것
public void login() {
//return "login";
}
}
indexjsp 복사해서 login.jsp 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Trinity Company - Home</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<link rel="shortcut icon" type="image/x-icon" href="assets/favicon.ico" />
<link rel="apple-touch-icon" sizes="57x57" href="assets/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="assets/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="assets/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="assets/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="assets/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="assets/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="assets/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="assets/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="assets/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="assets/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="assets/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="assets/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="assets/favicon-16x16.png">
<link rel="manifest" href="assets/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
<!-- Google fonts-->
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700" rel="stylesheet" type="text/css" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
</head>
<body id="page-top">
<!-- Navigation-->
<c:import url="menu.jsp"/>
<!-- Services-->
<section class="page-section" id="services">
<div class="container">
</div>
</section>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scripts.js"></script>
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-->
<!-- * * SB Forms JS * *-->
<!-- * * Activate your form at https://startbootstrap.com/solution/contact-forms * *-->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-->
<script src="https://cdn.startbootstrap.com/sb-forms-latest.js"></script>
</body>
</html>
login 만들기
<!-- login-->
<section class="page-section" id="services">
<div class="container">
<form action="./login" method="post">
<input type="text" name="id">
<input type="password" name="pw">
<button type="submit">초기화</button>
<button type="submit">로그인</button>
</form>
</div>
</section>
부트 스트랩으로 꾸미기(green 빌려옴)
<!-- LOGIN -->
<section class="page-section" id="login">
<div class="container">
<div class="row">
<div class="col-md-4 mx-auto">
<form action="./login" method="post">
<h2 class="text-center text-uppercase text-primary mb-4">LOGIN</h2>
<div class="form-group">
<!-- <label id="id"></label> -->
<input type="text" id ="id" name="id" class="form-control"
placeholder="아이디">
</div>
<div class="form-group">
<input type="password" id="pw" name="pw" class="form-control"
placeholder="비밀번호">
</div>
<div class="mb-3 row">
<div class="col-sm-12">
<div class="form-group text-center">
<button type="reset" class="btn btn-danger mr-2">초기화</button>
<button type="submit" class="btn btn-primary">로그인</button>
</div>
</div>
</div>
</form>
<div class="text-center">
<button type="button" id="join" class="btn btn-info">가입하기</button>
</div>
</div>
</div>
</div>
</section>
LoginController.java
package org.mask.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.mask.dto.LoginDTO;
import org.mask.service.LoginService;
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;
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
//2024.02.20 get/login
@GetMapping("/login")//로그인 페이지 열어주는 일만 할것
public void login() {
//return "login";
}
@PostMapping("/login")
public String login(HttpServletRequest request) {
String id = request.getParameter("id");
String pw = request.getParameter("pw");
// System.out.println("id: "+id+" / pw : "+pw);
LoginDTO loginDTO = new LoginDTO();
loginDTO.setId(id);
loginDTO.setPw(pw);
LoginDTO login = loginService.login(loginDTO);
if(login.getCount()==1) {
System.out.println("count : " + login.getCount());
System.out.println("name : " + login.getMname());
//세션만들기
HttpSession session = request.getSession();
session.setAttribute("mid", id);
session.setAttribute("mname", login.getMname());
}
return "redirect:/index";
}
//2024.02.20 로그아웃
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
HttpSession session = request.getSession();
if(session.getAttribute("mid") != null) {
session.removeAttribute("mid");
}
if(session.getAttribute("mname") != null) {
session.removeAttribute("mname");
}
session.invalidate();
return "redirect:/login";
}
}
LoginService.java 생성
package org.mask.service;
import org.mask.dao.LoginDAO;
import org.mask.dto.LoginDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//2024.02.20 생성
@Service
public class LoginService {
@Autowired
private LoginDAO loginDAO;
public LoginDTO login(LoginDTO dto) {
return loginDAO.login(dto);//컨트롤러로 가자
}
}
LoginDAO.java 생성
package org.mask.dao;
import org.apache.ibatis.session.SqlSession;
import org.mask.dto.LoginDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class LoginDAO {
@Autowired
private SqlSession sqlSession;
public LoginDTO login(LoginDTO dto) {
return sqlSession.selectOne("login.login", dto);//역으로> 다음엔 서비스 간다
}
}
login-mapper.xml 생성
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="login">
<select id="login" parameterType="loginDTO" resultType="loginDTO">
<![CDATA[
SELECT COUNT(*) as count, mname
FROM member
WHERE mid=#{id} AND mpw=#{pw} AND mgrade > 4
]]>
</select>
</mapper>
mybatis-config에
<typeAlias type="org.mask.dto.LoginDTO" alias="LoginDTO"/> 추가
<configuration>
<typeAliases>
<typeAlias type="java.lang.Integer" alias="integer"/>
<typeAlias type="org.mask.dto.BoardDTO" alias="boardDTO"/>
<typeAlias type="org.mask.dto.WriteDTO" alias="writeDTO"/>
<typeAlias type="org.mask.dto.CommentDTO" alias="commentDTO"/>
<typeAlias type="org.mask.dto.LoginDTO" alias="LoginDTO"/>
</typeAliases>
</configuration>
menu.jsp
로그인, 로그아웃, 마이인포 추가
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Navigation-->
<nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark" id="mainNav">
<div class="container">
<a class="navbar-brand" href="./index"><img
src="assets/android-icon-192x192.png" alt="..." /></a>
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse" data-bs-target="#navbarResponsive"
aria-controls="navbarResponsive" aria-expanded="false"
aria-label="Toggle navigation">
Menu <i class="fas fa-bars ms-1"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
<li class="nav-item"><a class="nav-link" href="./board">게시판</a></li>
<li class="nav-item"><a class="nav-link" href="./notice">공지사항</a></li><c:choose><c:when test="${sessionScope.mid ne null }">
<li class="nav-item"><a class="nav-link" href="./myInfo">${sessionScope.mname }님</a></li>
<li class="nav-item"><a class="nav-link" href="./login">로그인</a></li></c:when><c:otherwise></c:otherwise></c:choose>
<li class="nav-item"><a class="nav-link" href="./logout">로그아웃</a></li>
</ul>
</div>
</div>
</nav>
상단
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 태그 없애고,
다른 jsp에 아래 인크루드를 네비게이션 아래 입력
<!-- Navigation-->
<%@ include file="menu.jsp" %>
참고자료
https://m.blog.naver.com/halowd/221686425368
'2023.11.21-2024.05.31' 카테고리의 다른 글
20240221 스프링(로그인-글쓰기 버튼, 댓글쓰기, 수정,삭제, ip) (0) | 2024.02.21 |
---|---|
20240221 스프링(글쓰기, 댓글쓰기 ip, 엔터키처리) (1) | 2024.02.21 |
240220 스프링(댓글창, 댓글 출력, 페이징) (0) | 2024.02.20 |
20240219 스프링(웹표준 기술 - 모달 풋터,상세보기 모달,swal, 댓글창 만들기, 댓글 출력하기) (0) | 2024.02.19 |
20240216 스프링4일차(util, error,modal) (0) | 2024.02.16 |