From 95e3c1cfbaabc8cef0a51f33fcb88e171b3d46ac Mon Sep 17 00:00:00 2001 From: suhhyun <97878992+suhhyun524@users.noreply.github.com> Date: Fri, 12 May 2023 17:46:43 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=ED=99=95=EC=9D=B8=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zonebug/debugging/config/WebSecurityConfig.java | 2 +- .../debugging/controller/UserController.java | 13 ++++++++----- .../debugging/domain/user/UserRepository.java | 1 + .../debugging/dto/response/NicknameResponseDTO.java | 13 +++++++++++++ .../com/zonebug/debugging/service/UserService.java | 9 +++++++-- 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/zonebug/debugging/dto/response/NicknameResponseDTO.java diff --git a/src/main/java/com/zonebug/debugging/config/WebSecurityConfig.java b/src/main/java/com/zonebug/debugging/config/WebSecurityConfig.java index 3070faa..6feef18 100644 --- a/src/main/java/com/zonebug/debugging/config/WebSecurityConfig.java +++ b/src/main/java/com/zonebug/debugging/config/WebSecurityConfig.java @@ -62,7 +62,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .and() .authorizeHttpRequests((req) -> req - .requestMatchers("/user/authenticate", "/user/signup", "/user/signin", "/user/test/**").permitAll() + .requestMatchers("/user/authenticate", "/user/**").permitAll() .requestMatchers("/oauth/**").permitAll() .requestMatchers("/source/url").permitAll() .anyRequest().authenticated() diff --git a/src/main/java/com/zonebug/debugging/controller/UserController.java b/src/main/java/com/zonebug/debugging/controller/UserController.java index 8f6ea29..0bd9e5c 100644 --- a/src/main/java/com/zonebug/debugging/controller/UserController.java +++ b/src/main/java/com/zonebug/debugging/controller/UserController.java @@ -1,23 +1,19 @@ package com.zonebug.debugging.controller; import com.zonebug.debugging.domain.user.User; -import com.zonebug.debugging.dto.InfoUpdateDTO; import com.zonebug.debugging.dto.LoginDto; import com.zonebug.debugging.dto.TokenDto; import com.zonebug.debugging.dto.UserDto; -import com.zonebug.debugging.security.jwt.JwtFilter; +import com.zonebug.debugging.dto.response.NicknameResponseDTO; import com.zonebug.debugging.security.jwt.TokenProvider; -import com.zonebug.debugging.security.user.CustomUserDetails; import com.zonebug.debugging.service.UserService; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.core.Authentication; -import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import java.util.Optional; @@ -42,6 +38,13 @@ public ResponseEntity signIn(@Valid @RequestBody LoginDto loginDto) { return ResponseEntity.ok(userService.signIn(loginDto)); } + @GetMapping("/nickname") + public ResponseEntity checkNickname( + @RequestParam String nickname + ){ + return ResponseEntity.ok(userService.checkNickname(nickname)); + } + @PostMapping("/authenticate") public ResponseEntity authorize(@Valid @RequestBody LoginDto loginDto) { diff --git a/src/main/java/com/zonebug/debugging/domain/user/UserRepository.java b/src/main/java/com/zonebug/debugging/domain/user/UserRepository.java index 96bff5e..92ade41 100644 --- a/src/main/java/com/zonebug/debugging/domain/user/UserRepository.java +++ b/src/main/java/com/zonebug/debugging/domain/user/UserRepository.java @@ -8,5 +8,6 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); Optional findById(Long userId); + Optional findByNickname(String nickname); } diff --git a/src/main/java/com/zonebug/debugging/dto/response/NicknameResponseDTO.java b/src/main/java/com/zonebug/debugging/dto/response/NicknameResponseDTO.java new file mode 100644 index 0000000..cd4a729 --- /dev/null +++ b/src/main/java/com/zonebug/debugging/dto/response/NicknameResponseDTO.java @@ -0,0 +1,13 @@ +package com.zonebug.debugging.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +@AllArgsConstructor +public class NicknameResponseDTO { + + private boolean isPresent; +} diff --git a/src/main/java/com/zonebug/debugging/service/UserService.java b/src/main/java/com/zonebug/debugging/service/UserService.java index 79b001a..87b2912 100644 --- a/src/main/java/com/zonebug/debugging/service/UserService.java +++ b/src/main/java/com/zonebug/debugging/service/UserService.java @@ -2,11 +2,10 @@ import com.zonebug.debugging.domain.user.User; import com.zonebug.debugging.domain.user.UserRepository; -import com.zonebug.debugging.dto.InfoUpdateDTO; import com.zonebug.debugging.dto.LoginDto; import com.zonebug.debugging.dto.TokenDto; import com.zonebug.debugging.dto.UserDto; -import com.zonebug.debugging.security.user.CustomUserDetails; +import com.zonebug.debugging.dto.response.NicknameResponseDTO; import com.zonebug.debugging.security.util.SecurityUtil; import com.zonebug.debugging.security.jwt.TokenProvider; import lombok.RequiredArgsConstructor; @@ -54,6 +53,12 @@ public TokenDto signIn(LoginDto loginDto) { return tokenDto; } + public NicknameResponseDTO checkNickname(String nickname) { + Optional findUser = userRepository.findByNickname(nickname); + + return new NicknameResponseDTO(findUser.isPresent()); + + } @Transactional(readOnly = true) public Optional getCurrentUser() {