应用全局异常抛出
This commit is contained in:
@ -38,20 +38,15 @@ public class UserController {
|
|||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public ResponseEntity<?> registerUser(@Valid @RequestBody RegistrationDto registrationDto) {
|
public ResponseEntity<?> registerUser(@Valid @RequestBody RegistrationDto registrationDto) {
|
||||||
try {
|
|
||||||
|
|
||||||
UserEntity registeredUser = userService.registerNewUser(registrationDto);
|
UserEntity registeredUser = userService.registerNewUser(registrationDto);
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(registeredUser.getEmail()));
|
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(registeredUser.getEmail()));
|
||||||
} catch (Exception e) {
|
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.fail(ResultCode.BAD_REQUEST,null));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginDto loginDto) {
|
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginDto loginDto) {
|
||||||
try {
|
|
||||||
Authentication authentication = authenticationManager.authenticate(
|
Authentication authentication = authenticationManager.authenticate(
|
||||||
new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword())
|
new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword())
|
||||||
);
|
);
|
||||||
@ -64,13 +59,5 @@ public class UserController {
|
|||||||
tokenResponse.put("token", jwtToken);
|
tokenResponse.put("token", jwtToken);
|
||||||
|
|
||||||
return ResponseEntity.ok(ApiResponse.success(tokenResponse));
|
return ResponseEntity.ok(ApiResponse.success(tokenResponse));
|
||||||
|
|
||||||
} catch (BadCredentialsException e) {
|
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ApiResponse.fail(ResultCode.UNAUTHORIZED,null));
|
|
||||||
} catch (Exception e) {
|
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.fail(ResultCode.SERVICE_UNAVAILABLE,null));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package co.jp.app.service;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import co.jp.app.common.ResultCode;
|
||||||
|
import co.jp.app.exception.BusinessException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -30,10 +32,15 @@ public class UserService implements UserDetailsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws Exception {
|
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws BusinessException {
|
||||||
|
|
||||||
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
||||||
throw new Exception("error: Email" + registrationDto.getEmail() + " had been used");
|
throw new BusinessException(ResultCode.USER_EMAIL_ALREADY_EXISTS,"error: Email" + registrationDto.getEmail() + " had been used");
|
||||||
|
}
|
||||||
|
|
||||||
|
//密码最短6位限制
|
||||||
|
if (registrationDto.getPassword() == null || registrationDto.getPassword().length() < 6) {
|
||||||
|
throw new BusinessException(ResultCode.USER_PASSWORD_TOO_SHORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity newUser = new UserEntity();
|
UserEntity newUser = new UserEntity();
|
||||||
|
Reference in New Issue
Block a user