[Spring] 25. 회원가입개발자가 되기까지 (2023.08.16~2024.04.15)/[Spring] Basic Web2024. 1. 25. 03:11
Table of Contents
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:include page="../layout/header.jsp"></jsp:include>
<jsp:include page="../layout/nav.jsp"></jsp:include>
<div class="container my-3">
<h4 class="mb-3">회원가입</h4><br>
<form action="/member/register" method="post">
<div class="mb-3">
<label for="e" class="form-label">E-mail</label>
<input type="email" name="email" class="form-control" id="e" placeholder="example@example.com">
</div>
<div class="mb-3">
<label for="p" class="form-label">Password</label>
<input type="password" name="pwd" class="form-control" id="p" placeholder="password">
</div>
<div class="mb-3">
<label for="n" class="form-label">Nickname</label>
<input type="text" name="nickName" class="form-control" id="n">
</div>
<button type="submit" class="btn btn-primary">SignUp</button>
</form>
</div>
<jsp:include page="../layout/footer.jsp"></jsp:include>
MemberController.java
package com.basicWeb.www.controller;
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 com.basicWeb.www.security.MemberVO;
import com.basicWeb.www.service.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping("/member/*")
public class MemberController {
private final MemberService msv;
@GetMapping("/register")
public void register() {}
@PostMapping("/register")
public String register(MemberVO mvo) {
int isOk = msv.register(mvo);
return "index";
}
}
MemberService.interface
package com.basicWeb.www.service;
import com.basicWeb.www.security.MemberVO;
public interface MemberService {
int register(MemberVO mvo);
}
MemberServiceImpl.java
package com.basicWeb.www.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.basicWeb.www.repository.MemberDAO;
import com.basicWeb.www.security.MemberVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService {
private final MemberDAO mdao;
@Transactional
@Override
public int register(MemberVO mvo) {
int isOk = mdao.insert(mvo);
return mdao.insertAuthInit(mvo.getEmail());
}
}
회원가입을 하면 유저인지 관리자인지 구분하기 위해서 회원 가입을 할 때 auth도 함께 설정되도록 했다.
회원가입을 하면 처음엔 무조건 유저로 생성되며, 추후 관리자 계정은 admin으로 수정해줄 것이다.
MemberDAO.interface
package com.basicWeb.www.repository;
import com.basicWeb.www.security.MemberVO;
public interface MemberDAO {
int insert(MemberVO mvo);
int insertAuthInit(String email);
}
memberMapper.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="com.basicWeb.www.repository.MemberDAO">
<insert id="insert">
INSERT INTO member (email, pwd, nick_name)
VALUES(#{email}, #{pwd}, #{nickName})
</insert>
<insert id="insertAuthInit">
INSERT INTO auth_member
VALUES (#{email}, 'ROLE_USER')
</insert>
</mapper>
(+비밀번호 암호화)
package com.basicWeb.www.controller;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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 com.basicWeb.www.security.MemberVO;
import com.basicWeb.www.service.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping("/member/*")
public class MemberController {
private final MemberService msv;
private final BCryptPasswordEncoder bcEncoder;
@GetMapping("/register")
public void register() {}
@PostMapping("/register")
public String register(MemberVO mvo) {
mvo.setPwd(bcEncoder.encode(mvo.getPwd()));
int isOk = msv.register(mvo);
return "index";
}
}
[Spring] 25. 회원가입
(다음 게시물 예고편)
[Spring] 26. 로그인 / 로그아웃
728x90
@rlozlr :: 얼렁뚱땅 개발자
얼렁뚱땅 주니어 개발자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!