forked from UMC-EWHA/umc-spring-3rd
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
e89c6d3
commit cfc7449
Showing
30 changed files
with
604 additions
and
24 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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/umc/umcserver/domain/auth/controller/AuthController.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,36 @@ | ||
package com.umc.umcserver.domain.auth.controller; | ||
|
||
import com.umc.umcserver.domain.auth.dto.LoginRequestDto; | ||
import com.umc.umcserver.domain.auth.dto.LoginResponseDto; | ||
import com.umc.umcserver.domain.auth.service.AuthService; | ||
import com.umc.umcserver.global.dto.DtoMetaData; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
@RestController | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
// 로그인 | ||
@PostMapping("/login") | ||
public ResponseEntity<LoginResponseDto> login(@RequestBody LoginRequestDto requestDto) { | ||
DtoMetaData dtoMetaData; | ||
|
||
try { | ||
String token = authService.login(requestDto); | ||
dtoMetaData = new DtoMetaData("로그인 성공"); | ||
return ResponseEntity.ok(new LoginResponseDto(dtoMetaData, token)); | ||
} catch (Exception e) { | ||
dtoMetaData = new DtoMetaData(e.getMessage(), e.getClass().getName()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new LoginResponseDto(dtoMetaData)); | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/umc/umcserver/domain/auth/dto/LoginRequestDto.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,15 @@ | ||
package com.umc.umcserver.domain.auth.dto; | ||
|
||
import lombok.Data; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
@Data | ||
public class LoginRequestDto { | ||
|
||
private String email; | ||
private String password; | ||
|
||
public UsernamePasswordAuthenticationToken toAuthentication() { | ||
return new UsernamePasswordAuthenticationToken(email, password); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/umc/umcserver/domain/auth/dto/LoginResponseDto.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,21 @@ | ||
package com.umc.umcserver.domain.auth.dto; | ||
|
||
import com.umc.umcserver.global.dto.DtoMetaData; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class LoginResponseDto { | ||
|
||
private DtoMetaData dtoMetaData; | ||
private String token; | ||
|
||
public LoginResponseDto(DtoMetaData dtoMetaData, String token) { | ||
this.dtoMetaData = dtoMetaData; | ||
this.token = token; | ||
} | ||
|
||
public LoginResponseDto(DtoMetaData dtoMetaData) { | ||
this.dtoMetaData = dtoMetaData; | ||
this.token = null; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/umc/umcserver/domain/auth/repository/Account.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,32 @@ | ||
package com.umc.umcserver.domain.auth.repository; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@Entity | ||
public class Account { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column | ||
private String email; | ||
|
||
@Column | ||
private String password; | ||
|
||
@Builder | ||
public Account(Long id, String email, String password) { | ||
this.id = id; | ||
this.email = email; | ||
this.password = password; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/umc/umcserver/domain/auth/service/AuthService.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,34 @@ | ||
package com.umc.umcserver.domain.auth.service; | ||
|
||
import com.umc.umcserver.domain.auth.dto.LoginRequestDto; | ||
import com.umc.umcserver.global.jwt.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AuthService { | ||
private final AuthenticationManagerBuilder authenticationManagerBuilder; | ||
private final JwtTokenProvider tokenProvider; | ||
|
||
@Transactional | ||
public String login(LoginRequestDto requestDto) { | ||
// Login ID/PW 를 기반으로 AuthenticationToken 생성 | ||
UsernamePasswordAuthenticationToken authenticationToken = requestDto.toAuthentication(); | ||
|
||
// AuthenticationToken (유저 정보: 비밀번호) 검증 | ||
// authenticate 메서드가 실행이 될 때 CustomUserDetailsService 에서 만들었던 loadUserByUsername 메서드가 실행됨 | ||
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); | ||
|
||
// 인증 정보를 기반으로 JWT 토큰 생성 | ||
String token = tokenProvider.generateToken(authentication); | ||
|
||
// 토큰 발급 | ||
return token; | ||
} | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...umcserver/controller/BoardController.java → ...ain/board/controller/BoardController.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
2 changes: 1 addition & 1 deletion
2
.../umcserver/controller/TestController.java → ...main/board/controller/TestController.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
4 changes: 2 additions & 2 deletions
4
.../umc/umcserver/dto/CreatePostsReqDto.java → ...r/domain/board/dto/CreatePostsReqDto.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
2 changes: 1 addition & 1 deletion
2
.../umc/umcserver/dto/CreatePostsResDto.java → ...r/domain/board/dto/CreatePostsResDto.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
2 changes: 1 addition & 1 deletion
2
.../umc/umcserver/dto/DeletePostsReqDto.java → ...r/domain/board/dto/DeletePostsReqDto.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
2 changes: 1 addition & 1 deletion
2
.../umc/umcserver/dto/DeletePostsResDto.java → ...r/domain/board/dto/DeletePostsResDto.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,4 +1,4 @@ | ||
package com.umc.umcserver.dto; | ||
package com.umc.umcserver.domain.board.dto; | ||
|
||
import lombok.Getter; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...m/umc/umcserver/dto/FetchPostsReqDto.java → ...er/domain/board/dto/FetchPostsReqDto.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
6 changes: 2 additions & 4 deletions
6
...m/umc/umcserver/dto/FetchPostsResDto.java → ...er/domain/board/dto/FetchPostsResDto.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
4 changes: 2 additions & 2 deletions
4
.../umc/umcserver/dto/UpdatePostsReqDto.java → ...r/domain/board/dto/UpdatePostsReqDto.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
4 changes: 2 additions & 2 deletions
4
.../umc/umcserver/dto/UpdatePostsResDto.java → ...r/domain/board/dto/UpdatePostsResDto.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
2 changes: 1 addition & 1 deletion
2
...a/com/umc/umcserver/repository/Board.java → ...server/domain/board/repository/Board.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
2 changes: 1 addition & 1 deletion
2
...umcserver/repository/BoardRepository.java → ...ain/board/repository/BoardRepository.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
8 changes: 4 additions & 4 deletions
8
...m/umc/umcserver/service/BoardService.java → ...er/domain/board/service/BoardService.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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/umc/umcserver/global/dto/DtoMetaData.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,19 @@ | ||
package com.umc.umcserver.global.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DtoMetaData { | ||
private String message; | ||
private String exception; | ||
|
||
public DtoMetaData(String message) { | ||
this.message = message; | ||
this.exception = null; | ||
} | ||
|
||
public DtoMetaData(String message, String exception) { | ||
this.message = message; | ||
this.exception = exception; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/umc/umcserver/global/jwt/JwtAccessDeniedHandler.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,20 @@ | ||
package com.umc.umcserver.global.jwt; | ||
|
||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
// 유저 정보는 있지만 자원에 접근할 수 있는 권한이 없는 경우 403 에러 응답 | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/umc/umcserver/global/jwt/JwtAuthenticationEntryPoint.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,20 @@ | ||
package com.umc.umcserver.global.jwt; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
// 유요한 자격증명(유저 정보) 없이 접근하려 할 때 401 에러 응답 | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/umc/umcserver/global/jwt/JwtCustomFilter.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,48 @@ | ||
package com.umc.umcserver.global.jwt; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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 javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtCustomFilter extends OncePerRequestFilter { | ||
|
||
public static final String AUTHORIZATION_HEADER = "Authorization"; | ||
public static final String BEARER_PREFIX = "Bearer "; | ||
|
||
private final JwtTokenProvider tokenProvider; | ||
|
||
// 실제 필터링 로직: JWT 토큰의 인증 정보를 현재 쓰레드의 SecurityContext 에 저장 | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
// Request Header 에서 토큰 꺼냄 | ||
String token = resolveToken(request); | ||
// 토큰 유효성 검사 | ||
// 유요한 토큰이면 Authentication 을 가져와서 SecurityContext 에 저장 | ||
if(StringUtils.hasText(token) && tokenProvider.validateToken(token)) { | ||
Authentication authentication = tokenProvider.getAuthentication(token); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
// Request Header 에서 토큰 꺼내기 | ||
private String resolveToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader(AUTHORIZATION_HEADER); | ||
|
||
if(StringUtils.hasText(bearerToken) && bearerToken.startsWith(BEARER_PREFIX)) { | ||
return bearerToken.substring(7); | ||
} | ||
|
||
return null; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/umc/umcserver/global/jwt/JwtSecurityConfig.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,20 @@ | ||
package com.umc.umcserver.global.jwt; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.config.annotation.SecurityConfigurerAdapter; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.web.DefaultSecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
// TokenProvider 를 주입받은 JwtFilter 를 Security Filter 앞에 추가 | ||
@Override | ||
public void configure(HttpSecurity http) { | ||
JwtCustomFilter customJwtFilter = new JwtCustomFilter(jwtTokenProvider); | ||
http.addFilterBefore(customJwtFilter, UsernamePasswordAuthenticationFilter.class); | ||
} | ||
} |
Oops, something went wrong.