-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 로그인 #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[FEAT] 로그인 #37
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f09071
chore: temp rename to resolve casing issue
sungchaewon 21a460a
chore: rename FixLog to fixlog
sungchaewon 28e1b6c
merge: resolve conflicts with origin/develop
sungchaewon 1cf826b
fix(conflict): PostService 충돌 해결
sungchaewon 1e79909
fix(conflict): Post 관련 충돌 및 MainPageController 병합 완료
sungchaewon de99205
chore: FixLog 디렉토리 통일 및 로그인 관련 파일 복구, 충돌 해결 완료
sungchaewon 5fd66f1
feat(auth): JWT 로그인 기능 및 기본 프로필 설정 추가
sungchaewon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/FixLog/config/JwtAuthenticationFilter.java
This file contains hidden or 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,56 @@ | ||
| package com.example.FixLog.config; | ||
|
|
||
| import com.example.FixLog.domain.member.Member; | ||
| import com.example.FixLog.exception.CustomException; | ||
| import com.example.FixLog.repository.MemberRepository; | ||
| import com.example.FixLog.util.JwtUtil; | ||
| import java.io.IOException; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
| import com.example.FixLog.exception.ErrorCode; | ||
|
|
||
| public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
|
||
| private final JwtUtil jwtUtil; | ||
| private final MemberRepository memberRepository; | ||
|
|
||
| public JwtAuthenticationFilter(JwtUtil jwtUtil, MemberRepository memberRepository) { | ||
| this.jwtUtil = jwtUtil; | ||
| this.memberRepository = memberRepository; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain) | ||
| throws ServletException, IOException { | ||
|
|
||
| String token = resolveToken(request); | ||
|
|
||
| if (token != null && jwtUtil.isTokenValid(token)) { | ||
| String email = jwtUtil.getEmailFromToken(token); | ||
| Member member = memberRepository.findByEmail(email) | ||
| .orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); | ||
|
|
||
| Authentication auth = new UsernamePasswordAuthenticationToken(member, null, member.getAuthorities()); | ||
| SecurityContextHolder.getContext().setAuthentication(auth); | ||
| } | ||
|
|
||
| filterChain.doFilter(request, response); | ||
| } | ||
|
|
||
| private String resolveToken(HttpServletRequest request) { | ||
| String bearerToken = request.getHeader("Authorization"); | ||
| if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { | ||
| return bearerToken.substring(7); | ||
| } | ||
| return null; | ||
| } | ||
| } |
89 changes: 58 additions & 31 deletions
89
src/main/java/com/example/FixLog/config/SecurityConfig.java
This file contains hidden or 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,31 +1,58 @@ | ||
| //package com.example.FixLog.config; | ||
| // | ||
| //import org.springframework.context.annotation.Bean; | ||
| //import org.springframework.context.annotation.Configuration; | ||
| //import org.springframework.http.HttpMethod; | ||
| //import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| //import org.springframework.security.web.SecurityFilterChain; | ||
| //import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| //import org.springframework.security.crypto.password.PasswordEncoder; | ||
| // | ||
| //@Configuration | ||
| //public class SecurityConfig { | ||
| // | ||
| // @Bean | ||
| // public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| // http | ||
| // .csrf(csrf -> csrf.disable()) | ||
| // .authorizeHttpRequests(auth -> auth | ||
| // .requestMatchers(HttpMethod.POST, "/api/members/signup").permitAll() | ||
| // .requestMatchers(HttpMethod.GET, "/api/members/check-email").permitAll() | ||
| // .requestMatchers(HttpMethod.GET, "/api/members/check-nickname").permitAll() | ||
| // .anyRequest().authenticated() | ||
| // ); | ||
| // return http.build(); | ||
| // } | ||
| // | ||
| // @Bean | ||
| // public PasswordEncoder passwordEncoder() { | ||
| // return new BCryptPasswordEncoder(); | ||
| // } | ||
| //} | ||
| package com.example.FixLog.config; | ||
|
|
||
| import com.example.FixLog.repository.MemberRepository; | ||
| import com.example.FixLog.util.JwtUtil; | ||
| import jakarta.servlet.Filter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
|
||
| @Configuration | ||
| @RequiredArgsConstructor | ||
| public class SecurityConfig { | ||
|
|
||
| private final JwtUtil jwtUtil; | ||
| private final MemberRepository memberRepository; | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .csrf(csrf -> csrf.disable()) | ||
| .authorizeHttpRequests(auth -> auth | ||
| .requestMatchers(HttpMethod.POST, "/api/members/signup").permitAll() | ||
| .requestMatchers(HttpMethod.POST, "/api/auth/login").permitAll() | ||
| .requestMatchers(HttpMethod.GET, "/api/members/check-email").permitAll() | ||
| .requestMatchers(HttpMethod.GET, "/api/members/check-nickname").permitAll() | ||
| .requestMatchers(HttpMethod.GET, "/h2-console/**").permitAll() | ||
| .anyRequest().authenticated() | ||
| ) | ||
| .headers(headers -> headers.frameOptions(frame -> frame.disable())) // H2 콘솔용 | ||
| .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); | ||
|
|
||
| return http.build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public Filter jwtAuthenticationFilter() { | ||
| return new JwtAuthenticationFilter(jwtUtil, memberRepository); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
|
|
||
| // 인증 매니저 (선택: 로그인 시 AuthenticationManager 사용 가능) | ||
| @Bean | ||
| public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { | ||
| return config.getAuthenticationManager(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/FixLog/controller/AuthController.java
This file contains hidden or 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,23 @@ | ||
| package com.example.FixLog.controller; | ||
|
|
||
| import com.example.FixLog.dto.Response; | ||
| import com.example.FixLog.dto.member.LoginRequestDto; | ||
| import com.example.FixLog.dto.member.LoginResponseDto; | ||
| import com.example.FixLog.service.AuthService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/auth") | ||
| @RequiredArgsConstructor | ||
| public class AuthController { | ||
|
|
||
| private final AuthService authService; | ||
|
|
||
| @PostMapping("/login") | ||
| public ResponseEntity<Response<LoginResponseDto>> login(@RequestBody LoginRequestDto requestDto) { | ||
| LoginResponseDto result = authService.login(requestDto); | ||
| return ResponseEntity.ok(Response.success("로그인 성공", result)); | ||
| } | ||
| } |
91 changes: 53 additions & 38 deletions
91
src/main/java/com/example/FixLog/controller/MemberController.java
This file contains hidden or 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,38 +1,53 @@ | ||
| //package com.example.FixLog.controller; | ||
| // | ||
| // | ||
| //import com.example.FixLog.dto.Response; | ||
| //import com.example.FixLog.dto.member.SignupRequestDto; | ||
| //import com.example.FixLog.dto.member.DuplicateCheckResponseDto; | ||
| //import com.example.FixLog.service.MemberService; | ||
| //import lombok.RequiredArgsConstructor; | ||
| //import org.springframework.http.ResponseEntity; | ||
| //import org.springframework.web.bind.annotation.*; | ||
| // | ||
| //@RestController | ||
| //@RequestMapping("/api/members") | ||
| //@RequiredArgsConstructor | ||
| //public class MemberController { | ||
| // | ||
| // private final MemberService memberService; | ||
| // | ||
| // @PostMapping("/signup") | ||
| // public ResponseEntity<Response<String>> signup(@RequestBody SignupRequestDto request) { | ||
| // memberService.signup(request); | ||
| // return ResponseEntity.ok(Response.success("회원가입이 완료되었습니다.", null)); | ||
| // } | ||
| // | ||
| // @GetMapping("/check-email") | ||
| // public ResponseEntity<Response<DuplicateCheckResponseDto>> checkEmail(@RequestParam String email) { | ||
| // boolean exists = memberService.isEmailDuplicated(email); | ||
| // String msg = exists ? "이미 사용 중인 이메일입니다." : "사용 가능한 이메일입니다."; | ||
| // return ResponseEntity.ok(Response.success(msg, new DuplicateCheckResponseDto(exists))); | ||
| // } | ||
| // | ||
| // @GetMapping("/check-nickname") | ||
| // public ResponseEntity<Response<DuplicateCheckResponseDto>> checkNickname(@RequestParam String nickname) { | ||
| // boolean exists = memberService.isNicknameDuplicated(nickname); | ||
| // String msg = exists ? "이미 사용 중인 닉네임입니다." : "사용 가능한 닉네임입니다."; | ||
| // return ResponseEntity.ok(Response.success(msg, new DuplicateCheckResponseDto(exists))); | ||
| // } | ||
| //} | ||
| package com.example.FixLog.controller; | ||
|
|
||
|
|
||
| import com.example.FixLog.domain.member.Member; | ||
| import com.example.FixLog.dto.Response; | ||
| import com.example.FixLog.dto.member.MemberInfoResponseDto; | ||
| import com.example.FixLog.dto.member.SignupRequestDto; | ||
| import com.example.FixLog.dto.member.DuplicateCheckResponseDto; | ||
| import com.example.FixLog.service.MemberService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/members") | ||
| @RequiredArgsConstructor | ||
| public class MemberController { | ||
|
|
||
| private final MemberService memberService; | ||
|
|
||
| @PostMapping("/signup") | ||
| public ResponseEntity<Response<String>> signup(@RequestBody SignupRequestDto request) { | ||
| memberService.signup(request); | ||
| return ResponseEntity.ok(Response.success("회원가입 성공", null)); | ||
| } | ||
|
|
||
| @GetMapping("/check-email") | ||
| public ResponseEntity<Response<DuplicateCheckResponseDto>> checkEmail(@RequestParam String email) { | ||
| boolean exists = memberService.isEmailDuplicated(email); | ||
| String msg = exists ? "이미 사용 중인 이메일입니다." : "사용 가능한 이메일입니다."; | ||
| return ResponseEntity.ok(Response.success(msg, new DuplicateCheckResponseDto(exists))); | ||
| } | ||
|
|
||
| @GetMapping("/check-nickname") | ||
| public ResponseEntity<Response<DuplicateCheckResponseDto>> checkNickname(@RequestParam String nickname) { | ||
| boolean exists = memberService.isNicknameDuplicated(nickname); | ||
| String msg = exists ? "이미 사용 중인 닉네임입니다." : "사용 가능한 닉네임입니다."; | ||
| return ResponseEntity.ok(Response.success(msg, new DuplicateCheckResponseDto(exists))); | ||
| } | ||
|
|
||
| @GetMapping("/me") | ||
| public ResponseEntity<Response<MemberInfoResponseDto>> getMyInfo(@AuthenticationPrincipal Member member) { | ||
| MemberInfoResponseDto responseDto = new MemberInfoResponseDto( | ||
| member.getEmail(), | ||
| member.getNickname(), | ||
| member.getProfileImageUrl(), | ||
| member.getBio(), | ||
| member.getSocialType() | ||
| ); | ||
| return ResponseEntity.ok(Response.success("회원 정보 조회 성공", responseDto)); | ||
| } | ||
| } |
This file contains hidden or 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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/FixLog/dto/member/LoginRequestDto.java
This file contains hidden or 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,9 @@ | ||
| package com.example.FixLog.dto.member; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class LoginRequestDto { | ||
| private String email; | ||
| private String password; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/FixLog/dto/member/LoginResponseDto.java
This file contains hidden or 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,15 @@ | ||
| package com.example.FixLog.dto.member; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| @Getter | ||
| @AllArgsConstructor | ||
| public class LoginResponseDto { | ||
| private Long userId; | ||
| private String accessToken; | ||
| private String nickname; | ||
| private String profileImageUrl; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/FixLog/dto/member/MemberInfoResponseDto.java
This file contains hidden or 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,15 @@ | ||
| package com.example.FixLog.dto.member; | ||
|
|
||
| import com.example.FixLog.domain.member.SocialType; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class MemberInfoResponseDto { | ||
| private String email; | ||
| private String nickname; | ||
| private String profileImageUrl; | ||
| private String bio; | ||
| private SocialType socialType; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -21,7 +21,9 @@ public enum ErrorCode { | |
| FOLDER_NOT_FOUND(HttpStatus.NOT_FOUND, "폴더를 찾을 수 없습니다."), | ||
| ACCESS_DENIED(HttpStatus.FORBIDDEN, "권한이 없습니다."), | ||
| TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 태그 번호입니다."), | ||
| SORT_NOT_EXIST(HttpStatus.BAD_REQUEST, "사용할 수 없는 정렬입니다."); | ||
| SORT_NOT_EXIST(HttpStatus.BAD_REQUEST, "사용할 수 없는 정렬입니다."), | ||
| INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."), | ||
| MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 사용자입니다."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기에 있는 MEMBER NOT FOUND 이미 있는 에러일 것같아요!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 그럼 로그인시에 회원 정보 없음도 둘 중에 한 에러로 통일하겠습니다! |
||
|
|
||
| private final HttpStatus status; | ||
| private final String message; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.