forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
server/src/main/java/com/capstone/server/controller/JwtLoginApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.capstone.server.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.capstone.server.dto.login.JoinRequestDto; | ||
import com.capstone.server.dto.login.LoginRequestDto; | ||
import com.capstone.server.dto.login.LoginResponseDto; | ||
import com.capstone.server.model.UserEntity; | ||
import com.capstone.server.response.SuccessResponse; | ||
import com.capstone.server.service.JwtTokenService; | ||
import com.capstone.server.service.UserService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/user") | ||
public class JwtLoginApiController { | ||
@Value("${jwt.secretKey}") | ||
private String secretKey; | ||
@Value("${jwt.expireTime}") | ||
private long expireTime; | ||
|
||
@Autowired | ||
private UserService userService; | ||
@Autowired | ||
private JwtTokenService jwtTokenService; | ||
|
||
@PostMapping("/join") | ||
public ResponseEntity<?> join(@RequestBody JoinRequestDto joinRequestDto) { | ||
|
||
// loginId 중복 체크 | ||
userService.checkLoginIdDuplicate(joinRequestDto.getLoginId()); | ||
|
||
// 회원가입 | ||
userService.join(joinRequestDto); | ||
return ResponseEntity.ok().body(new SuccessResponse("Join Success")); | ||
} | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<?> login(@RequestBody LoginRequestDto loginRequestDto) { | ||
// 로그인 | ||
UserEntity user = userService.login(loginRequestDto); | ||
|
||
// Jwt Token 발급 | ||
String jwtToken = jwtTokenService.createToken(user.getLoginId(), secretKey, expireTime); | ||
|
||
LoginResponseDto loginResponseDto = new LoginResponseDto(user, jwtToken); | ||
return ResponseEntity.ok().body(new SuccessResponse(loginResponseDto)); | ||
} | ||
|
||
@GetMapping("/info") | ||
public ResponseEntity<?> userInfo(Authentication auth) { | ||
UserEntity loginUser = userService.getLoginUserByLoginId(auth.getName()); | ||
|
||
return ResponseEntity.ok().body(new SuccessResponse(String.format("loginId : %s, role : %s", | ||
loginUser.getLoginId(), loginUser.getRole().name()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 49 additions & 17 deletions
66
server/src/main/java/com/capstone/server/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,74 @@ | ||
package com.capstone.server.service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.postgresql.shaded.com.ongres.scram.common.message.ServerFinalMessage.Error; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.capstone.server.code.ErrorCode; | ||
import com.capstone.server.dto.UserUpdateRequestDto; | ||
import com.capstone.server.dto.login.JoinRequestDto; | ||
import com.capstone.server.dto.login.LoginRequestDto; | ||
import com.capstone.server.exception.CustomException; | ||
import com.capstone.server.model.UserEntity; | ||
import com.capstone.server.model.enums.UserRole; | ||
import com.capstone.server.repository.UserRepository; | ||
|
||
@Service | ||
public class UserService { | ||
@Autowired | ||
private UserRepository userRepository; | ||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
@Value("${admin.loginId}") | ||
private String adminLoginId; | ||
|
||
public UserEntity createUser(UserEntity userEntity) { | ||
try { | ||
return userRepository.save(userEntity); | ||
} catch (Exception e) { | ||
throw new CustomException(ErrorCode.USER_EXISTS, e); | ||
// 중복된 ID 체크 | ||
public void checkLoginIdDuplicate(String loginId) { | ||
|
||
if (userRepository.existsByLoginId(loginId)) { | ||
throw new CustomException(ErrorCode.DUPLICATE_USER_LOGIN_ID); | ||
} | ||
} | ||
|
||
public List<UserEntity> getAllUsers() { | ||
return userRepository.findAll(); | ||
// 회원가입 | ||
@Transactional | ||
public UserEntity join(JoinRequestDto joinRequestDto) { | ||
if (!joinRequestDto.getLoginId().contains(adminLoginId)) { | ||
throw new CustomException(ErrorCode.USER_NOT_ADMIN); | ||
} | ||
UserEntity user = userRepository.save(joinRequestDto.toEntity(passwordEncoder.encode(joinRequestDto.getPassword()))); | ||
|
||
user.setRole(UserRole.ADMIN); | ||
return user; | ||
} | ||
|
||
public UserEntity updateUserNameById(Long userId, UserUpdateRequestDto userUpdateRequestDto) { | ||
Optional<UserEntity> existingUserOptional = userRepository.findById(userId); | ||
if (existingUserOptional.isPresent()) { | ||
UserEntity existingUser = existingUserOptional.get(); | ||
existingUser.setName(userUpdateRequestDto.getName()); | ||
return userRepository.save(existingUser); | ||
} else { | ||
// 로그인 | ||
public UserEntity login(LoginRequestDto loginRequestDto) { | ||
Optional<UserEntity> optionalUser = userRepository.findByLoginId(loginRequestDto.getLoginId()); | ||
if (optionalUser.isEmpty()) { | ||
throw new CustomException(ErrorCode.USER_NOT_FOUND); | ||
} | ||
|
||
UserEntity user = optionalUser.get(); | ||
|
||
if (!passwordEncoder.matches(loginRequestDto.getPassword(), user.getPassword())) { | ||
throw new CustomException(ErrorCode.USER_NOT_MATCH_PASSWORD); | ||
} | ||
|
||
return user; | ||
} | ||
|
||
// user 찾기 | ||
public UserEntity getLoginUserByLoginId(String loginId) { | ||
if (loginId == null) return null; | ||
|
||
Optional<UserEntity> optionalUser = userRepository.findByLoginId(loginId); | ||
if (optionalUser.isEmpty()) return null; | ||
|
||
return optionalUser.get(); | ||
} | ||
} |