-
Notifications
You must be signed in to change notification settings - Fork 5
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
[BE] 행사 관리자 비밀번호 추가 #213
Merged
Merged
[BE] 행사 관리자 비밀번호 추가 #213
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
860eb78
feat: 이벤트 비밀번호 추
Arachneee 41716eb
test: 테스트 공통 설정 클래스 분리
Arachneee 5a63ee0
feat: 어드민 인터셉터 추가 및 jwt 설정 추가
Arachneee 7fbf55a
feat: 이벤트 로그인 기능 구현
Arachneee 67415df
feat: 쿠키 설정 분리
Arachneee b0b84d4
submodule 업데이트
Arachneee 5698db3
style: 주석 제거
Arachneee 6869156
refactor: 로컬 환경 쿠키 secure 옵션 제거
Arachneee f50b6fb
test: 접근제어자 수정
Arachneee 8cdd084
test: 개행 추가
Arachneee 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 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
40 changes: 40 additions & 0 deletions
40
server/src/main/java/server/haengdong/application/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,40 @@ | ||
package server.haengdong.application; | ||
|
||
|
||
import java.util.Map; | ||
import server.haengdong.domain.TokenProvider; | ||
import server.haengdong.exception.AuthenticationException; | ||
|
||
public class AuthService { | ||
|
||
private static final String TOKEN_NAME = "eventToken"; | ||
private static final String CLAIM_SUB = "sub"; | ||
|
||
private final TokenProvider tokenProvider; | ||
|
||
public AuthService(TokenProvider tokenProvider) { | ||
this.tokenProvider = tokenProvider; | ||
} | ||
|
||
public String createToken(String eventId) { | ||
Map<String, Object> payload = Map.of(CLAIM_SUB, eventId); | ||
|
||
return tokenProvider.createToken(payload); | ||
} | ||
|
||
public String findEventIdByToken(String token) { | ||
validateToken(token); | ||
Map<String, Object> payload = tokenProvider.getPayload(token); | ||
return (String) payload.get(CLAIM_SUB); | ||
} | ||
|
||
private void validateToken(String token) { | ||
if (!tokenProvider.validateToken(token)) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
|
||
public String getTokenName() { | ||
return TOKEN_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
4 changes: 4 additions & 0 deletions
4
server/src/main/java/server/haengdong/application/request/EventLoginAppRequest.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,4 @@ | ||
package server.haengdong.application.request; | ||
|
||
public record EventLoginAppRequest(String token, String password) { | ||
} |
44 changes: 44 additions & 0 deletions
44
server/src/main/java/server/haengdong/config/AdminInterceptor.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,44 @@ | ||
package server.haengdong.config; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import server.haengdong.application.AuthService; | ||
import server.haengdong.exception.AuthenticationException; | ||
import server.haengdong.infrastructure.auth.AuthenticationExtractor; | ||
|
||
@Slf4j | ||
public class AdminInterceptor implements HandlerInterceptor { | ||
|
||
private final AuthService authService; | ||
private final AuthenticationExtractor authenticationExtractor; | ||
|
||
public AdminInterceptor(AuthService authService, AuthenticationExtractor authenticationExtractor) { | ||
this.authService = authService; | ||
this.authenticationExtractor = authenticationExtractor; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
log.trace("login request = {}", request.getRequestURI()); | ||
|
||
String method = request.getMethod(); | ||
if (method.equals("GET")) { | ||
return true; | ||
} | ||
|
||
validateToken(request); | ||
|
||
return true; | ||
} | ||
|
||
private void validateToken(HttpServletRequest request) { | ||
String token = authenticationExtractor.extract(request, authService.getTokenName()); | ||
String tokenEventId = authService.findEventIdByToken(token); | ||
String eventId = request.getRequestURI().split("/")[2]; | ||
if (!tokenEventId.equals(eventId)) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
server/src/main/java/server/haengdong/config/WebConfig.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
server/src/main/java/server/haengdong/config/WebMvcConfig.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,65 @@ | ||
package server.haengdong.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import server.haengdong.application.AuthService; | ||
import server.haengdong.domain.TokenProvider; | ||
import server.haengdong.infrastructure.auth.AuthenticationExtractor; | ||
import server.haengdong.infrastructure.auth.JwtTokenProvider; | ||
import server.haengdong.infrastructure.auth.TokenProperties; | ||
|
||
@RequiredArgsConstructor | ||
@EnableConfigurationProperties(TokenProperties.class) | ||
@Configuration | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
|
||
private final TokenProperties tokenProperties; | ||
|
||
@Value("${cors.max-age}") | ||
private Long maxAge; | ||
|
||
@Value("${cors.allowed-origins}") | ||
private String[] allowedOrigins; | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins(allowedOrigins) | ||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS") | ||
.allowedHeaders("*") | ||
.maxAge(maxAge); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(adminInterceptor()) | ||
.addPathPatterns("/api/**") | ||
.excludePathPatterns("/api/events"); | ||
} | ||
|
||
@Bean | ||
public AdminInterceptor adminInterceptor() { | ||
return new AdminInterceptor(authService(), authenticationExtractor()); | ||
} | ||
|
||
@Bean | ||
public AuthService authService() { | ||
return new AuthService(tokenProvider()); | ||
} | ||
|
||
@Bean | ||
public TokenProvider tokenProvider() { | ||
return new JwtTokenProvider(tokenProperties); | ||
} | ||
|
||
@Bean | ||
public AuthenticationExtractor authenticationExtractor() { | ||
return new AuthenticationExtractor(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
server/src/main/java/server/haengdong/domain/TokenProvider.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,12 @@ | ||
package server.haengdong.domain; | ||
|
||
import java.util.Map; | ||
|
||
public interface TokenProvider { | ||
|
||
String createToken(Map<String, Object> payload); | ||
|
||
Map<String, Object> getPayload(String token); | ||
|
||
boolean validateToken(String token); | ||
} |
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: 4 additions & 0 deletions
4
server/src/main/java/server/haengdong/exception/AuthenticationException.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,4 @@ | ||
package server.haengdong.exception; | ||
|
||
public class AuthenticationException extends RuntimeException { | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c: 정규표현식이나 다른 방식으로 해야할 것 같은데 뾰족한 방법이 없는 것 같네요..