Skip to content

Commit

Permalink
refactor : 회원가입 로그인 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
MyunghyunNero committed Nov 18, 2023
1 parent 5c9619f commit 402ee32
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 99 deletions.
17 changes: 17 additions & 0 deletions src/main/java/kusitms/gallae/config/PasswordConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kusitms.gallae.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
public class PasswordConfig {

@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
}
15 changes: 0 additions & 15 deletions src/main/java/kusitms/gallae/config/SpringSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,14 @@ public class SpringSecurityConfig {
private final JwtProvider jwtAuthenticationProvider;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final UserDetailsService customUserDetailsService;

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers( "/swagger-resources/**",
"/swagger-ui/**",
"/v3/api-docs",
"/webjars/**");
}
@Bean
public AuthenticationManager authenticationManagerBean(HttpSecurity http) throws Exception {
return http.getSharedObject(AuthenticationManagerBuilder.class)
.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder())
.and()
.build();
}



@Bean
public SecurityFilterChain SecurityFilterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package kusitms.gallae.controller;

import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletResponse;
import kusitms.gallae.config.BaseResponse;
import kusitms.gallae.config.BaseResponseStatus;
import kusitms.gallae.dto.user.LoginRequestDto;
import kusitms.gallae.dto.user.LoginResponse;
import kusitms.gallae.service.user.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -16,16 +20,32 @@ public class AuthenticationController {
@Autowired
private AuthenticationService authenticationService;

@Autowired
private PasswordEncoder passwordEncoder;


// 로그인 요청을 처리하는 메서드
@PostMapping("/login")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequestDto loginRequestDto,
HttpServletResponse httpServletResponse) {
public ResponseEntity<BaseResponse<LoginResponse>> login(
@Parameter(description = "아이디", example = "아이")
@RequestParam(value = "loginId", required = true)
String loginId,

@Parameter(description = "패스워드")
@RequestParam(value = "login password", required = true)
String loginPw,

HttpServletResponse httpServletResponse) {
try {
LoginResponse loginResponse = authenticationService.login(loginRequestDto, httpServletResponse);
return ResponseEntity.ok(loginResponse);

LoginRequestDto loginRequestDto = new LoginRequestDto();
loginRequestDto.setLoginId(loginId);
loginRequestDto.setLoginPw(loginPw);
System.out.println(loginRequestDto.getLoginPw());
return ResponseEntity.ok(new BaseResponse<>(authenticationService.login(loginRequestDto, httpServletResponse)));
} catch (RuntimeException e) {

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
return ResponseEntity.ok(new BaseResponse<>(BaseResponseStatus.NOT_FOUND));
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/kusitms/gallae/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kusitms.gallae.controller;

import kusitms.gallae.config.BaseResponseStatus;
import kusitms.gallae.domain.User;
import kusitms.gallae.dto.user.UserRegistrationDto;
import kusitms.gallae.service.user.UserService;
Expand All @@ -21,8 +22,8 @@ public UserController(UserService userService) {
@PostMapping("/register")
public ResponseEntity<?> registerUser(@ModelAttribute UserRegistrationDto registrationDto) {
try {
User user = userService.registerNewUser(registrationDto);
return ResponseEntity.ok(user);
userService.registerNewUser(registrationDto);
return ResponseEntity.ok(BaseResponseStatus.SUCCESS);
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/kusitms/gallae/dto/user/LoginRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class LoginRequestDto {
private String loginId;
Expand Down
18 changes: 1 addition & 17 deletions src/main/java/kusitms/gallae/dto/user/LoginResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,11 @@
@Getter
@NoArgsConstructor
public class LoginResponse {
private Long id;
private String nickName;
private String email;
private String name;
private String imageUrl;
private String phoneNumber;
private Role role;
private String tokenType;
private String accessToken;
private String refreshToken;

@Builder
public LoginResponse(Long id, String name, String phoneNumber, String nickName, String email, String imageUrl, Role role, String tokenType, String accessToken, String refreshToken) {
this.id = id;
this.nickName = nickName;
this.phoneNumber = phoneNumber;
this.name = name;
this.email = email;
this.imageUrl = imageUrl;
this.role = role;
this.tokenType = tokenType;
public LoginResponse(String accessToken, String refreshToken) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByLoginId(String loginId);
boolean existsByNickName(String nickName);
Optional<User> findByLoginId(String loginId);

Optional<User> findByLoginIdAndLoginPw(String loginId, String loginPw);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AuthenticationService {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private JwtProvider jwtProvider;
Expand All @@ -28,13 +27,8 @@ public class AuthenticationService {
private UserRepository userRepository;

public LoginResponse login(LoginRequestDto loginRequestDto, HttpServletResponse httpServletResponse) {
// 사용자 인증
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequestDto.getLoginId(), loginRequestDto.getLoginId())
);

// 사용자 정보 조회
User user = userRepository.findByLoginId(loginRequestDto.getLoginId())
User user = userRepository.findByLoginIdAndLoginPw(loginRequestDto.getLoginId(), loginRequestDto.getLoginPw())
.orElseThrow(() -> new RuntimeException("User not found"));

// JWT 액세스 토큰 생성
Expand All @@ -49,14 +43,6 @@ public LoginResponse login(LoginRequestDto loginRequestDto, HttpServletResponse

// 로그인 응답 생성 및 반환
return LoginResponse.builder()
.id(user.getId())
.name(user.getName())
.phoneNumber(user.getPhoneNumber())
.nickName(user.getNickName())
.email(user.getEmail())
.imageUrl(user.getProfileImageUrl())
.role(user.getRole())
.tokenType("Bearer")
.accessToken(accessToken)
.refreshToken(user.getRefreshToken())
.build();
Expand Down

This file was deleted.

23 changes: 12 additions & 11 deletions src/main/java/kusitms/gallae/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
@Service
public class UserService {

private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final S3Service s3Service;

@Autowired
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, S3Service s3Service) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.s3Service = s3Service;
}
private UserRepository userRepository;

@Autowired
private PasswordEncoder passwordEncoder;

public User registerNewUser(UserRegistrationDto registrationDto) throws IllegalStateException, IOException {
@Autowired
private S3Service s3Service;

public void registerNewUser(UserRegistrationDto registrationDto) throws IllegalStateException, IOException {
if (userRepository.existsByLoginId(registrationDto.getLoginId())) {
throw new IllegalStateException("이미 존재하는 ID 입니다.");
}
Expand All @@ -46,9 +45,11 @@ public User registerNewUser(UserRegistrationDto registrationDto) throws IllegalS
.refreshToken("") // 회원가입은 토큰 없음
.profileImageUrl(profileImageUrl) // 프로필 이미지 URL 추가
.signUpStatus(User.UserSignUpStatus.USER)
.loginPw(passwordEncoder.encode(registrationDto.getLoginPw()))
.loginPw(registrationDto.getLoginPw())
.build();

return userRepository.save(newUser);
System.out.println(newUser.getLoginPw());

userRepository.save(newUser);
}
}

0 comments on commit 402ee32

Please sign in to comment.