ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • spring security+jwt 회원가입, 로그인 #1
    SOLUX-완숙이 2022. 1. 12. 15:50

    security 적용 전 회원가입 코드

     

    1. Member 클래스

    package solux.wansuki.OurNeighbor_BE.domain.Member;
    
    import lombok.*;
    
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import java.util.stream.Collectors;
    
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    @Entity
    public class Member {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        private String name;
    
        private String nickName;
    
        private String email;
    
        private String password;
    
        private String loginId;
    
        @ElementCollection(fetch = FetchType.EAGER)
        @Builder.Default
        private List<String> roles = new ArrayList<>();
    
    
        @Builder
        public Member(String name, String nickName, String email, String password,String loginId, List<String> roles) {
            this.name = name;
            this.nickName = nickName;
            this.email = email;
            this.password = password;
            this.loginId = loginId;
            this.roles = roles;
        }

     

    2. MemberService

    package solux.wansuki.OurNeighbor_BE.service;
    
    import lombok.RequiredArgsConstructor;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import solux.wansuki.OurNeighbor_BE.domain.Member.Member;
    import solux.wansuki.OurNeighbor_BE.domain.Member.MemberRepository;
    import solux.wansuki.OurNeighbor_BE.dto.Member.MemberSaveDto;
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    
    @RequiredArgsConstructor
    @Service
    public class MemberService {
        private final MemberRepository memberRepository;
    
        @Transactional
        public Long signUp(MemberSaveDto memberSaveDto) {
            Member member = Member.builder()
                    .name(memberSaveDto.getName())
                    .nickName(memberSaveDto.getNickName())
                    .email(memberSaveDto.getEmail())
                    .password(memberSaveDto.getPassword())
                    .loginId(memberSaveDto.getLoginId())
                    .roles(Collections.singletonList("ROLE_USER"))
                    .build();
            return memberRepository.save(member).getId();
        }
    
        public List<Member> findAll() {
            return memberRepository.findAll();
        }
    }

    회원가입 시 role을 ROLE_USER로 설정

     

    3. MemberController

    package solux.wansuki.OurNeighbor_BE.controller;
    
    import lombok.RequiredArgsConstructor;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    import solux.wansuki.OurNeighbor_BE.domain.Member.Member;
    import solux.wansuki.OurNeighbor_BE.dto.Member.MemberSaveDto;
    import solux.wansuki.OurNeighbor_BE.service.MemberService;
    
    import java.util.List;
    
    @RequiredArgsConstructor
    @RestController
    public class MemberController {
        private final MemberService memberService;
    
        @PostMapping("/signup")
        public Long signUp(@RequestBody MemberSaveDto memberSaveDto) {
            return memberService.signUp(memberSaveDto);
        }
    
        @GetMapping("/member")
        public List<Member> findAll() {
            return memberService.findAll();
        }
    }
Designed by Tistory.