-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev_backend' into fix/#298
- Loading branch information
Showing
27 changed files
with
959 additions
and
105 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
64 changes: 64 additions & 0 deletions
64
...end/src/main/java/com/now/naaga/auth/presentation/interceptor/ManagerAuthInterceptor.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,64 @@ | ||
package com.now.naaga.auth.presentation.interceptor; | ||
|
||
import com.now.naaga.auth.exception.AuthException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static com.now.naaga.auth.exception.AuthExceptionType.*; | ||
|
||
@Component | ||
public class ManagerAuthInterceptor implements HandlerInterceptor { | ||
|
||
public static final int AUTH_HEADER_INFO_SIZE = 2; | ||
|
||
public static final String AUTH_HEADER_TYPE = "Basic "; | ||
|
||
@Value("${manager.id}") | ||
private String id; | ||
|
||
@Value("${manager.password}") | ||
private String password; | ||
|
||
@Override | ||
public boolean preHandle(final HttpServletRequest request, | ||
final HttpServletResponse response, | ||
final Object handler) throws Exception { | ||
final List<String> idAndPassword = extractHeaderInfo(request); | ||
return loginForManager(idAndPassword); | ||
} | ||
|
||
private List<String> extractHeaderInfo(final HttpServletRequest request) { | ||
final String header = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
if (header == null) { | ||
throw new AuthException(NOT_EXIST_HEADER); | ||
} | ||
final String decodedHeader = new String(Base64.getDecoder().decode(header)); | ||
if (!decodedHeader.startsWith(AUTH_HEADER_TYPE)) { | ||
throw new AuthException(INVALID_HEADER); | ||
} | ||
final String decodedHeaderWithoutType = decodedHeader.replace(AUTH_HEADER_TYPE, ""); | ||
final String[] idAndPassword = decodedHeaderWithoutType.split(":"); | ||
if (idAndPassword.length != AUTH_HEADER_INFO_SIZE) { | ||
throw new AuthException(INVALID_HEADER); | ||
} | ||
return Arrays.asList(idAndPassword); | ||
} | ||
|
||
private boolean loginForManager(final List<String> idAndPassword) { | ||
final String inputId = idAndPassword.get(0).strip(); | ||
final String inputPassword = idAndPassword.get(1).strip(); | ||
if (Objects.equals(id, inputId) && Objects.equals(password, inputPassword)) { | ||
return true; | ||
} | ||
throw new AuthException(INVALID_MANAGER); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/now/naaga/common/presentation/interceptor/RequestMatcher.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,18 @@ | ||
package com.now.naaga.common.presentation.interceptor; | ||
|
||
import org.springframework.util.AntPathMatcher; | ||
import org.springframework.util.PathMatcher; | ||
|
||
|
||
public class RequestMatcher { | ||
|
||
private static final PathMatcher pathMatcher = new AntPathMatcher(); | ||
|
||
private RequestMatcher() {} | ||
|
||
public static boolean match(final RequestPattern requestPattern, | ||
final RequestPattern inputRequestPattern) { | ||
return pathMatcher.match(requestPattern.path(), inputRequestPattern.path()) | ||
&& requestPattern.method() == inputRequestPattern.method(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...rc/main/java/com/now/naaga/common/presentation/interceptor/RequestMatcherInterceptor.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,80 @@ | ||
package com.now.naaga.common.presentation.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RequestMatcherInterceptor implements HandlerInterceptor { | ||
|
||
private static final List<HttpMethod> ALL_HTTP_METHODS = new ArrayList<>(List.of(HttpMethod.values())); | ||
|
||
private final HandlerInterceptor handlerInterceptor; | ||
|
||
private final List<RequestPattern> includedRequestPatterns = new ArrayList<>(); | ||
|
||
private final List<RequestPattern> excludedRequestPatterns = new ArrayList<>(); | ||
|
||
|
||
public RequestMatcherInterceptor(final HandlerInterceptor handlerInterceptor) { | ||
this.handlerInterceptor = handlerInterceptor; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(final HttpServletRequest request, | ||
final HttpServletResponse response, | ||
final Object handler) throws Exception { | ||
if (isIncludedRequestPattern(request)) { | ||
return handlerInterceptor.preHandle(request, response, handler); | ||
} | ||
return true; | ||
} | ||
|
||
private boolean isIncludedRequestPattern(final HttpServletRequest request) { | ||
final String requestPath = request.getServletPath(); | ||
final String requestMethod = request.getMethod(); | ||
|
||
final boolean isIncludedPattern = includedRequestPatterns.stream() | ||
.anyMatch(requestPattern -> requestPattern.match(requestPath, requestMethod)); | ||
|
||
final boolean isNotExcludedPattern = excludedRequestPatterns.stream() | ||
.noneMatch(requestPattern -> requestPattern.match(requestPath, requestMethod)); | ||
|
||
return isIncludedPattern && isNotExcludedPattern; | ||
} | ||
|
||
public RequestMatcherInterceptor includeRequestPattern(final String requestPathPattern, | ||
final HttpMethod... requestMethods) { | ||
final List<HttpMethod> mappingRequestMethods = decideRequestMethods(requestMethods); | ||
|
||
for (HttpMethod httpMethod : mappingRequestMethods) { | ||
this.includedRequestPatterns.add(new RequestPattern(requestPathPattern, httpMethod)); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public RequestMatcherInterceptor excludeRequestPattern(final String requestPathPattern, | ||
final HttpMethod... requestMethods) { | ||
final List<HttpMethod> mappingRequestMethods = decideRequestMethods(requestMethods); | ||
|
||
for (HttpMethod httpMethod : mappingRequestMethods) { | ||
this.excludedRequestPatterns.add(new RequestPattern(requestPathPattern, httpMethod)); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private List<HttpMethod> decideRequestMethods(final HttpMethod[] requestMethods) { | ||
final List<HttpMethod> httpMethods = new ArrayList<>(List.of(requestMethods)); | ||
|
||
if (httpMethods.isEmpty()) { | ||
httpMethods.addAll(ALL_HTTP_METHODS); | ||
} | ||
|
||
return httpMethods; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/now/naaga/common/presentation/interceptor/RequestPattern.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.now.naaga.common.presentation.interceptor; | ||
|
||
import org.springframework.http.HttpMethod; | ||
|
||
public record RequestPattern(String path, | ||
HttpMethod method) { | ||
|
||
public boolean match(final String inputPath, | ||
final String inputMethodAsString) { | ||
final HttpMethod inputMethod = HttpMethod.valueOf(inputMethodAsString.toUpperCase()); | ||
final RequestPattern inputRequestPattern = new RequestPattern(inputPath, inputMethod); | ||
|
||
return RequestMatcher.match(this, inputRequestPattern); | ||
} | ||
} |
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.