[B-3-1] 회원가입

2020. 1. 19. 20:03Project B (SPMS)/Project B 파트8

반응형

지금까지 JUnit으로 아이디 생성 및 아이디별 권한 데이터를 생성했었다.

여기서는 회원가입 페이지부터 회원가입 처리까지 되도록 하겠다.

AOP를 활용해서 소스코드를 수정하면 좋지만, 현재 기능동작까지만 되게끔만 해놓겠다.

설명은 나중에 리뷰할 때 작성하겠다. 딱히 어렵지도 않은 부분이고, 귀찮으므로..

 

로그인 페이지 수정

views/

customLogin.jsp

 

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>SB Admin 2 - Bootstrap Admin Theme</title>

<!-- Bootstrap Core CSS -->
<link href="/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="/resources/vendor/metisMenu/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="/resources/dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="/resources/vendor/font-awesome/css/font-awesome.min.css"
	rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

	<div class="container">
		<div class="row">
			<div class="col-md-4 col-md-offset-4">
				<div class="login-panel panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title">Please Sign In</h3>
					</div>
					<div class="panel-body">
						<form role="form" method='post' action="/login">
							<fieldset>
								<div class="form-group">
									<input class="form-control" placeholder="userid"
										name="username" type="text" autofocus>
								</div>
								<div class="form-group">
									<input class="form-control" placeholder="Password"
										name="password" type="password" value="">
								</div>
								<div class="checkbox">
									<label> <input name="remember-me" type="checkbox">Remember Me
									</label>
								</div>
								<!-- Change this to a button or input when using this as a form -->
								
							</fieldset>
							<input type="hidden" name="${_csrf.parameterName}"
								value="${_csrf.token}" />
						</form>
						<a href="index.html" class="btn btn-lg btn-success btn-block">Login</a><br/>
								<div>New here? <a href="/customSignup">Create an Account</a>
                                </div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- jQuery -->
	<script src="/resources/vendor/jquery/jquery.min.js"></script>

	<!-- Bootstrap Core JavaScript -->
	<script src="/resources/vendor/bootstrap/js/bootstrap.min.js"></script>

	<!-- Metis Menu Plugin JavaScript -->
	<script src="/resources/vendor/metisMenu/metisMenu.min.js"></script>

	<!-- Custom Theme JavaScript -->
	<script src="/resources/dist/js/sb-admin-2.js"></script>
  <script>
  
  $(".btn-success").on("click", function(e){
    
    e.preventDefault();
    $("form").submit();
    
  });
  
  </script>
  
<c:if test="${param.logout != null}">
      <script>
      $(document).ready(function(){
      	alert("로그아웃하였습니다.");
      });
      </script>
</c:if>  

</body>

</html>

 

 


 

회원가입 페이지 생성

 

views/

customSignup.jsp

 

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>SB Admin 2 - Bootstrap Admin Theme</title>

<!-- Bootstrap Core CSS -->
<link href="/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="/resources/vendor/metisMenu/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="/resources/dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="/resources/vendor/font-awesome/css/font-awesome.min.css"
	rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

	<div class="container">
		<div class="row">
			<div class="col-md-4 col-md-offset-4">
				<div class="login-panel panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title">Please Sign In</h3>
					</div>
					<div class="panel-body">
						<form role="form" method='post' action="/customSignup">
							<fieldset>
								<div class="form-group">
									<input class="form-control" placeholder="사용자 아이디" id="user_id" name="userid" type="text" autofocus="autofocus" required="required" >
								</div>
								<div class="form-group">
									<input class="form-control" placeholder="비밀번호"
										name="userpw" id="userpw" type="password" value="" required="required">
								</div>
								<div class="form-group">
									<input class="form-control" placeholder="사용자 명" name="userName" id="user_name" type="text" value="" required="required">
								</div>
								<!-- Change this to a button or input when using this as a form -->
								<a href="#" class="btn btn-lg btn-primary btn-block" id="sign-up-btn">Set Up Your Account</a>
							</fieldset>
							<input type="hidden" name="${_csrf.parameterName}"
								value="${_csrf.token}" />
						</form>

					</div>
				</div>
			</div>
		</div>
	</div>



	<!-- jQuery -->
	<script src="/resources/vendor/jquery/jquery.min.js"></script>

	<!-- Bootstrap Core JavaScript -->
	<script src="/resources/vendor/bootstrap/js/bootstrap.min.js"></script>

	<!-- Metis Menu Plugin JavaScript -->
	<script src="/resources/vendor/metisMenu/metisMenu.min.js"></script>

	<!-- Custom Theme JavaScript -->
	<script src="/resources/dist/js/sb-admin-2.js"></script>
  <script>
  $(".btn-primary").on("click", function(e){
	  
		/* 폼 유효성 검사 */
		if($("#user_id").val() === ""){
			alert("아이디를 입력해주세요.");
			$("#user_id").focus();
			return;
		}else if($("#userpw").val() === ""){
			alert("패스워드를 입력해주세요.");
			$("#userpw").focus();
			return;
		}else if($("#user_name").val() === ""){
	  		alert("사용자 명을 입력해주세요.");
	  		$("#user_name").focus();
			return;
		}
    e.preventDefault();
    $("form").submit();
    alert("회원가입을 축하드립니다.");
  });
  
  </script>

</body>

</html>

회원가입 작성 페이지 이동 및  처리

 

src/main/java

com.spms.controller

CommonController.java

@GetMapping("/customSignup")

@PostMapping("/customSignup")

 

더보기
package com.spms.controller;

import org.springframework.security.core.Authentication;
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.servlet.mvc.support.RedirectAttributes;

import com.spms.domain.MemberVO;
import com.spms.service.CommonService;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;

@Controller
@Log4j
@AllArgsConstructor
public class CommonController {

	private CommonService commonService;
	
	@GetMapping("/accessError")
	public void accessDenied(Authentication auth, Model model) {

		log.info("access Denied : " + auth);

		model.addAttribute("msg", "Access Denied");
	}

	@GetMapping("/customLogin")
	public void loginInput(String error, String logout, Model model) {

		log.info("error: " + error);
		log.info("logout: " + logout);

		if (error != null) {
			model.addAttribute("error", "Login Error Check Your Account");
		}

		if (logout != null) {
			model.addAttribute("logout", "Logout!!");
		}
	}

	@GetMapping("/customLogout")
	public void logoutGET() {

		log.info("custom logout");
	}

	@PostMapping("/customLogout")
	public void logoutPost() {

		log.info("post custom logout");
	}

	@GetMapping("/customSignup")
	public void signupGET() {
		log.info("회원가입 폼");
	}
	
	@PostMapping("/customSignup")
	public String signupPOST(MemberVO member, RedirectAttributes rttr) {
		log.info(member);
		commonService.customSignup(member);
		rttr.addFlashAttribute("result", member.getUserName());
		return "redirect:/board/list";
	}	
	
}

 


src/main/java

com.spms.service

CommonService.java 인터페이스

 

더보기
package com.spms.service;

import com.spms.domain.MemberVO;

public interface CommonService {
	public void customSignup(MemberVO member);
}

 


src/main/java

com.spms.service

CommonServiceImpl.java

 

더보기
package com.spms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spms.domain.MemberVO;
import com.spms.mapper.MemberMapper;

import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@Log4j
@Service
@AllArgsConstructor
public class CommonServiceImpl implements CommonService {

	@Setter(onMethod_ = @Autowired)
	private PasswordEncoder pwEncoder;
	private MemberMapper memberMapper;
	
	@Transactional
	@Override
	public void customSignup(MemberVO member) {
		member.setUserpw(pwEncoder.encode(member.getUserpw()));
		memberMapper.createMember(member);
		memberMapper.createMemberAuth(member);
	}

}

 


src/main/java

com.spms.mapper

MemberMapper.java

 

더보기
package com.spms.mapper;

import com.spms.domain.MemberVO;

public interface MemberMapper {
	public MemberVO read(String userid);
	public void createMember(MemberVO member);
	public void createMemberAuth(MemberVO member);
}

 


src/main/resources

com

spms

mapper

MemberMapper.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.spms.mapper.MemberMapper">
	<resultMap type="com.spms.domain.MemberVO" id="memberMap">
		<id property="userid" column="userid"/>
		<result property="userid" column="userid"/>
		<result property="userpw" column="userpw"/>
		<result property="userName" column="username"/>
		<result property="regDate" column="regdate"/>
		<result property="updateDate" column="updatedate"/>
		<collection property="authList" resultMap="authMap">
		</collection> 
	</resultMap>
  
	<resultMap type="com.spms.domain.AuthVO" id="authMap">
		<result property="userid" column="userid"/>
		<result property="auth" column="auth"/>
	</resultMap>
  
	<select id="read" resultMap="memberMap">
		SELECT 
			mem.userid,  userpw, username, enabled, regdate, updatedate, auth
		FROM 
			tbl_member mem LEFT OUTER JOIN tbl_member_auth auth on mem.userid = auth.userid 
		WHERE mem.userid = #{userid} 
	</select>
	
	<!-- 등록 (회원) -->
	<insert id="createMember">
		insert into tbl_member (
			USERID, USERPW, USERNAME, REGDATE, UPDATEDATE, ENABLED
		) 
		values (
			#{userid}, #{userpw}, #{userName}, SYSDATE, SYSDATE, '1'
		)
	</insert>

	<!-- 등록 (회원 권한) -->
	<insert id="createMemberAuth">
		insert into TBL_MEMBER_AUTH (
			USERID, AUTH
		) values (
			#{userid}, 'ROLE_USER'
		)
	</insert>
		
</mapper>

 

 


로그인 창 하단 부분에 계정생성하는 부분을 알 수 있다.

 

클릭하면, 계정생성하는 창에 뜬다.

 

로그인 창

 


회원가입 창

회원가입 창

반응형

'Project B (SPMS) > Project B 파트8' 카테고리의 다른 글

[B-3-2] 댓글과 댓글 수에 대한 처리  (0) 2020.01.19