목차

회원가입 구현

테이블 생성

DB 관련 클래스 생성(Entity, DTO Repository)

DB 저장

프로젝트 구조

Gihub 링크


members 테이블 생성

CREATE TABLE `members` (
  `ID` int NOT NULL AUTO_INCREMENT COMMENT '고유번호',
  `LOGIN_ID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '아이디',
  `LOGIN_PWD` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '비밀번호',
  `ROLE` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '권한',
  `NICKNAME` varchar(15) DEFAULT NULL COMMENT '닉네임',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


Entity 생성

Members.java

package com.example.login.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
@Entity
public class Members {

    // 고유번호
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    // 아이디
    @Column(nullable = false, updatable = false)
    private String loginId;

    // 비밀번호
    @Column(nullable = false)
    private String loginPwd;

    // 권한
    @Column(nullable = false)
    private String role;
    
    // 닉네임
    @Column(nullable = false)
    private String nickname;
    
    
    
    @Builder
    public Members(String loginId, String loginPwd, String role, String nickname) {
        this.loginId    = loginId;
        this.loginPwd   = loginPwd;
        this.role       = role;
        this.nickname   = nickname;
    }

}


DTO 생성

MembersDto.java

package com.example.login.dto;

import com.example.login.entity.Members;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class MembersDto {

    private String loginId;     // 아이디
    private String loginPwd;    // 비밀번호
    private String nickname;    // 닉네임
    private String role;        // 권한


    public Members toEntity() {
        Members builder = Members.builder()
                .loginId(loginId)
                .loginPwd(loginPwd)
                .nickname(nickname)
                .role(role) 
                .build();
        return builder;
    }
    
}


Repository 생성

MembersRepository.java

package com.example.login.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.login.entity.Members;

public interface MembersRepository extends JpaRepository<Members, Integer> {

    // 회원 존재 여부 확인
    boolean existsByLoginId(String LoginId);

    // 회원 정보 조회
    Members findByLoginId(String LoginId);

}


Service 생성

JoinService.java

package com.example.login.service;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.example.login.dto.MembersDto;
import com.example.login.repository.MembersRepository;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class JoinService {

    private final MembersRepository membersRepository;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    
    /**
     * 회원가입
     * @param membersDto
     * @return
     */
    public boolean joinProc(MembersDto membersDto) {

        // 회원 존재 여부 확인
        boolean isMembers = membersRepository.existsByLoginId(membersDto.getLoginId());

        if (!isMembers) {
            // 회원 저장
            membersDto.setLoginPwd(bCryptPasswordEncoder.encode(membersDto.getLoginPwd())); // 시큐리티 암호화
            membersDto.setRole("ROLE_USER");  // or "ROLE_ADMIN"

            membersRepository.save(membersDto.toEntity());
        }

        return isMembers;
    }
    
}


컨트롤러 수정

JoinController.java

import org.springframework.web.bind.annotation.PostMapping;

import com.example.login.dto.MembersDto;
import com.example.login.service.JoinService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Controller
public class JoinController {

    private final JoinService joinService;

    /**
     * 회원가입 > 저장
     * @param membersDto
     * @return
     */
    @PostMapping("/joinProc")
    public String joinProc(MembersDto membersDto) {

        if (!joinService.joinProc(membersDto)) {
            return "redirect:/";
        }
        else {
            return "redirect:/join";
        }

    }

}


프로젝트 구조



Gihub 링크

전체 소스 다운로드

©2024, DevDream