diff --git a/backend/build.gradle b/backend/build.gradle index d0c4526..18dfcf3 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -20,8 +20,8 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' -// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'org.springframework.boot:spring-boot-starter-security' // 필요할 때 주석 해제하고 사용 + implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.3' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' @@ -42,4 +42,4 @@ test { bootRun { String activeProfile = System.properties['spring.profiles.active'] systemProperty "spring.profiles.active", activeProfile -} \ No newline at end of file +} diff --git a/backend/src/main/java/com/infp/ciat/config/security/RequestLoginDTO.java b/backend/src/main/java/com/infp/ciat/config/security/RequestLoginDTO.java deleted file mode 100644 index 17f3022..0000000 --- a/backend/src/main/java/com/infp/ciat/config/security/RequestLoginDTO.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.infp.ciat.config.security; - -import lombok.NoArgsConstructor; - -/*** - * 로그인 요청 dto - */ -@NoArgsConstructor -public class RequestLoginDTO { - private String email; - private String password; - - public String getEmail() { - return email; - } - - public String getPassword() { - return password; - } - - public void setEmail(String email) { - this.email = email; - } - - public void setPassword(String password) { - this.password = password; - } - - public RequestLoginDTO(String email, String password) { - this.email = email; - this.password = password; - } -} diff --git a/backend/src/main/java/com/infp/ciat/config/security/WebSecurityConfig.java b/backend/src/main/java/com/infp/ciat/config/security/WebSecurityConfig.java index dcc3fd7..918d1fd 100644 --- a/backend/src/main/java/com/infp/ciat/config/security/WebSecurityConfig.java +++ b/backend/src/main/java/com/infp/ciat/config/security/WebSecurityConfig.java @@ -1,26 +1,28 @@ package com.infp.ciat.config.security; -import com.infp.ciat.user.service.AccountService; +//import com.infp.ciat.user.service.OAuth2DetailesService; +//import com.infp.ciat.user.service.OAuth2DetailesService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.transaction.annotation.Transactional; /*** * 스프링시큐리티 설정 */ + +@Transactional(readOnly = true) @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { - @Autowired - private AccountService customUserDetailService; + +// @Autowired +// private OAuth2DetailesService oAuth2DetailesService; /*** * default 패스워드 암호화알고리즘 사용 설정 @@ -38,37 +40,20 @@ public PasswordEncoder passwordEncoder(){ */ @Override protected void configure(HttpSecurity http) throws Exception { - Loginfilter loginfilter = new Loginfilter(authenticationManagerBean()); - loginfilter.setFilterProcessesUrl("/signin"); - + http.csrf().disable(); http .authorizeRequests() - .anyRequest().permitAll() - .and() - .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) - .and() - .csrf().disable() - .addFilter(loginfilter); - } - - /*** - * 사용자 userdetailservice 등록 - * @param auth - * @throws Exception - */ - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.userDetailsService(customUserDetailService); - } +// .antMatchers("/user/**").authenticated() // Q + .anyRequest().permitAll() + .and() + .formLogin() + .usernameParameter("email") + .passwordParameter("password"); - /*** - * login 필터를 위한 authenticationManager Bean으로 등록 - * @return - * @throws Exception - */ - @Bean - @Override - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); +// +// http +// .oauth2Login() +// .userInfoEndpoint() +// .userService(oAuth2DetailesService); } } diff --git a/backend/src/main/java/com/infp/ciat/user/controller/AccountController.java b/backend/src/main/java/com/infp/ciat/user/controller/AccountController.java index 7744303..0376358 100644 --- a/backend/src/main/java/com/infp/ciat/user/controller/AccountController.java +++ b/backend/src/main/java/com/infp/ciat/user/controller/AccountController.java @@ -20,10 +20,11 @@ public class AccountController { private final AccountService accountService; @PostMapping("/signup") - public ResponseEntity SignUp(@Valid @RequestBody SignupRequestDTO requestDTO) { + public ResponseEntity signUp(@Valid @RequestBody SignupRequestDTO requestDTO) { + log.info("------------------- 회원가입 요청 -----------------------"); - Long created_id = accountService.SignUp(requestDTO); + Long created_id = accountService.signUp(requestDTO); return new ResponseEntity<>(new SignUpResponse(created_id), HttpStatus.CREATED); } } diff --git a/backend/src/main/java/com/infp/ciat/user/controller/dto/request/SignupRequestDTO.java b/backend/src/main/java/com/infp/ciat/user/controller/dto/request/SignupRequestDTO.java index 9c45aa3..8111db0 100644 --- a/backend/src/main/java/com/infp/ciat/user/controller/dto/request/SignupRequestDTO.java +++ b/backend/src/main/java/com/infp/ciat/user/controller/dto/request/SignupRequestDTO.java @@ -1,13 +1,19 @@ package com.infp.ciat.user.controller.dto.request; +import com.infp.ciat.user.entity.Account; +import com.infp.ciat.user.entity.Role; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import javax.validation.constraints.NotEmpty; @Getter @Setter +@NoArgsConstructor public class SignupRequestDTO { + @NotEmpty private String email; @@ -16,4 +22,21 @@ public class SignupRequestDTO { @NotEmpty private String nickname; + + @Builder + public SignupRequestDTO(String email, String password, String nickname) { + this.email = email; + this.password = password; + this.nickname = nickname; + } + + public Account toEntity() { + return Account.builder() + .email(email) + .password(password) + .nickname(nickname) + .role(Role.ROLE_USER) + .build(); + } + } diff --git a/backend/src/main/java/com/infp/ciat/user/entity/Account.java b/backend/src/main/java/com/infp/ciat/user/entity/Account.java index 76d6f8c..d574d35 100644 --- a/backend/src/main/java/com/infp/ciat/user/entity/Account.java +++ b/backend/src/main/java/com/infp/ciat/user/entity/Account.java @@ -8,12 +8,14 @@ /*** * 유저 Entity */ -@Builder + @NoArgsConstructor +@Setter @Getter @Entity @ToString public class Account extends BaseTimeEntity { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -31,9 +33,13 @@ public class Account extends BaseTimeEntity { @Column(nullable = false) private Role role; +// private String provider; +// private String providerId; + + + @Builder - public Account(Long id, String email, String nickname, String password, Role role) { - this.id = id; + public Account(String email, String nickname, String password, Role role) { this.email = email; this.nickname = nickname; this.password = password; diff --git a/backend/src/main/java/com/infp/ciat/user/repository/AccountRepository.java b/backend/src/main/java/com/infp/ciat/user/repository/AccountRepository.java index 3845f87..7efdb9c 100644 --- a/backend/src/main/java/com/infp/ciat/user/repository/AccountRepository.java +++ b/backend/src/main/java/com/infp/ciat/user/repository/AccountRepository.java @@ -6,5 +6,7 @@ import java.util.Optional; public interface AccountRepository extends JpaRepository { - Optional findByEmail(String email); + + // select * from account where email = ? + Account findByEmail(String email); } diff --git a/backend/src/main/java/com/infp/ciat/user/service/AccountService.java b/backend/src/main/java/com/infp/ciat/user/service/AccountService.java index 609ec90..ce74586 100644 --- a/backend/src/main/java/com/infp/ciat/user/service/AccountService.java +++ b/backend/src/main/java/com/infp/ciat/user/service/AccountService.java @@ -1,27 +1,28 @@ package com.infp.ciat.user.service; -import com.infp.ciat.user.controller.dto.AccountContext; import com.infp.ciat.user.controller.dto.request.SignupRequestDTO; import com.infp.ciat.user.entity.Account; -import com.infp.ciat.user.entity.Role; import com.infp.ciat.user.repository.AccountRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -@Service @RequiredArgsConstructor +@Service @Transactional(readOnly = true) @Slf4j -public class AccountService implements UserDetailsService { +public class AccountService { + private final AccountRepository accountRepository; + private final PasswordEncoder passwordEncoder; + + /*** * 회원가입서비스 * 회원중복검사는 JPA가 수행하여 생략 @@ -29,22 +30,12 @@ public class AccountService implements UserDetailsService { * @param requestdto */ @Transactional - public Long SignUp(SignupRequestDTO requestdto){ - Account new_account = Account.builder() - .email(requestdto.getEmail()) - .nickname(requestdto.getNickname()) - .password(passwordEncoder.encode(requestdto.getPassword())) - .role(Role.ROLE_USER) - .build(); - - return accountRepository.save(new_account).getId(); - } + public Long signUp(SignupRequestDTO requestdto){ - @Override - public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { - Account find_user = accountRepository.findByEmail(email) - .orElseThrow(() -> new UsernameNotFoundException("사용자가 없습니다")); - log.debug(String.format("%s 계정 로그인 시도", email)); - return AccountContext.FromAccountToAccountContext(find_user); + requestdto.setPassword(passwordEncoder.encode(requestdto.getPassword())); + Account accountEntity = requestdto.toEntity(); + + return accountRepository.save(accountEntity).getId(); } + } diff --git a/backend/src/test/java/com/infp/ciat/user/controller/AccountControllerTest.java b/backend/src/test/java/com/infp/ciat/user/controller/AccountControllerTest.java deleted file mode 100644 index 9722754..0000000 --- a/backend/src/test/java/com/infp/ciat/user/controller/AccountControllerTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.infp.ciat.user.controller; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.infp.ciat.user.controller.dto.request.SignupRequestDTO; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.servlet.MockMvc; - -import static org.junit.jupiter.api.Assertions.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@ActiveProfiles("test") -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@AutoConfigureMockMvc -class AccountControllerTest { - @Autowired - private MockMvc mockMvc; - - @Test - @DisplayName("회원가입") - public void SignUp() throws Exception { - SignupRequestDTO signup_requestdto1 = create_signup_requestdto("test1@test.com", "test1", "password"); - - mockMvc.perform( - post("/signup") - .contentType(MediaType.APPLICATION_JSON) - .content(new ObjectMapper().writeValueAsString(signup_requestdto1)) - ) - .andExpect(status().isCreated()) - .andDo(print()); - } - - /*** - * 회원가입 요청 DTO 생성 - * @param email - * @param nickname - * @param password - * @return - */ - private SignupRequestDTO create_signup_requestdto(String email, String nickname, String password) { - SignupRequestDTO signupRequestDTO = new SignupRequestDTO(); - signupRequestDTO.setEmail(email); - signupRequestDTO.setNickname(nickname); - signupRequestDTO.setPassword(password); - - return signupRequestDTO; - } -} \ No newline at end of file diff --git a/backend/src/test/java/com/infp/ciat/user/service/AccountServiceTest.java b/backend/src/test/java/com/infp/ciat/user/service/AccountServiceTest.java index 0370fd1..b82e85a 100644 --- a/backend/src/test/java/com/infp/ciat/user/service/AccountServiceTest.java +++ b/backend/src/test/java/com/infp/ciat/user/service/AccountServiceTest.java @@ -1,80 +1,69 @@ package com.infp.ciat.user.service; import com.infp.ciat.user.controller.dto.request.SignupRequestDTO; -import static org.junit.jupiter.api.Assertions.*; +import com.infp.ciat.user.entity.Account; import com.infp.ciat.user.repository.AccountRepository; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.test.context.ActiveProfiles; -import org.springframework.transaction.annotation.Transactional; + +import javax.transaction.Transactional; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; @ActiveProfiles("test") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Transactional class AccountServiceTest { - @Autowired - AccountService accountService; + @Autowired + AccountService accountService; - @Autowired - AccountRepository accountRepository; + @Autowired + AccountRepository accountRepository; - @AfterEach - public void aftereach() { - try { - accountRepository.deleteAllInBatch(); - } catch (Exception e){ - // hibernate error 무시 - } + @Autowired + PasswordEncoder passwordEncoder; + + @AfterEach + public void aftereach() { + try { + accountRepository.deleteAllInBatch(); + } catch (Exception e){ + // hibernate error 무시 } + } - @Test - @DisplayName("한명 회원가입 테스트") - public void signin_one(){ - SignupRequestDTO signup_requestdto = create_signup_requestdto("test1@test.com", "test1", "password"); - accountService.SignUp(signup_requestdto); + @Test + void 회원가입() { - } + String email = "test@test.com"; + String password = "test_password"; + String nickname = "test_nickname"; - @Test - @DisplayName("두명 회원가입 테스트") - public void signin_two(){ - SignupRequestDTO signup_requestdto1 = create_signup_requestdto("test1@test.com", "test1", "password"); - accountService.SignUp(signup_requestdto1); - SignupRequestDTO signup_requestdto2 = create_signup_requestdto("test2@test.com", "test2", "password"); -// assertThrows(DataIntegrityViolationException.class, () -> accountService.SignUp(signup_requestdto2)); - accountService.SignUp(signup_requestdto2); - } + //given + SignupRequestDTO signupRequestDTO = SignupRequestDTO.builder() + .email(email) + .password(password) + .nickname(nickname) + .build(); - @Test - @DisplayName("중복 회원가입") - public void signin_duplicate(){ - SignupRequestDTO signup_requestdto1 = create_signup_requestdto("test1@test.com", "test1", "password"); - accountService.SignUp(signup_requestdto1); + //when + accountService.signUp(signupRequestDTO); + List all = accountRepository.findAll(); - SignupRequestDTO signup_requestdto2 = create_signup_requestdto("test1@test.com", "test1", "password"); - assertThrows(DataIntegrityViolationException.class, () -> accountService.SignUp(signup_requestdto2)); - } + //then + assertThat(all.get(0).getEmail()).isEqualTo(email); + assertThat(passwordEncoder.matches(password, all.get(0).getPassword())).isTrue(); + assertThat(all.get(0).getNickname()).isEqualTo(nickname); - /*** - * 회원가입 요청 DTO 생성 - * @param email - * @param nickname - * @param password - * @return - */ - private SignupRequestDTO create_signup_requestdto(String email, String nickname, String password) { - SignupRequestDTO signupRequestDTO = new SignupRequestDTO(); - signupRequestDTO.setEmail(email); - signupRequestDTO.setNickname(nickname); - signupRequestDTO.setPassword(password); - - return signupRequestDTO; - } + } -} \ No newline at end of file +}