-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 이벤트 비밀번호 추 * test: 테스트 공통 설정 클래스 분리 * feat: 어드민 인터셉터 추가 및 jwt 설정 추가 * feat: 이벤트 로그인 기능 구현 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> * feat: 쿠키 설정 분리 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> * submodule 업데이트 * style: 주석 제거 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> * refactor: 로컬 환경 쿠키 secure 옵션 제거 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> * test: 접근제어자 수정 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> * test: 개행 추가 Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]> --------- Co-authored-by: 3juhwan <[email protected]> Co-authored-by: kunsanglee <[email protected]> Co-authored-by: khabh <[email protected]>
- Loading branch information
1 parent
b2258ac
commit 7c6f2c4
Showing
40 changed files
with
563 additions
and
234 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
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.