-
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.
Browse files
Browse the repository at this point in the history
화이트리스트 요청 시 토큰 검증 생략 로직 구현
- Loading branch information
Showing
5 changed files
with
106 additions
and
34 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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/munecting/api/global/util/AllowedPathPatternProvider.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,46 @@ | ||
package com.munecting.api.global.util; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.server.PathContainer; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.pattern.PathPattern; | ||
import org.springframework.web.util.pattern.PathPatternParser; | ||
|
||
import java.util.Arrays; | ||
|
||
@Component | ||
public class AllowedPathPatternProvider { | ||
|
||
private final PathPatternParser patternParser; | ||
private final String[] whitelistPatterns; | ||
|
||
public AllowedPathPatternProvider( | ||
PathPatternParser patternParser, | ||
@Value("${spring.security.whitelist.patterns}") | ||
String[] whitelistPatterns | ||
) { | ||
this.patternParser = patternParser; | ||
this.whitelistPatterns = whitelistPatterns; | ||
} | ||
|
||
public boolean isPathWhitelisted(final String path) { | ||
PathContainer requestPath = parsePathContainer(path); | ||
|
||
return Arrays.stream(whitelistPatterns) | ||
.map(this::parsePathPattern) | ||
.anyMatch(whitePathPattern -> whitePathPattern.matches(requestPath)); | ||
} | ||
|
||
private PathPattern parsePathPattern(String whitePattern) { | ||
return patternParser.parse(whitePattern); | ||
} | ||
|
||
private PathContainer parsePathContainer(String path) { | ||
return PathContainer.parsePath(path); | ||
} | ||
|
||
public String[] getWhitelistPatterns() { | ||
return whitelistPatterns; | ||
} | ||
|
||
} |
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
34 changes: 34 additions & 0 deletions
34
src/test/java/com/munecting/api/global/util/AllowedPathPatternProviderTest.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.munecting.api.global.util; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
class AllowedPathPatternProviderTest { | ||
|
||
@Autowired | ||
private AllowedPathPatternProvider patternProvider; | ||
|
||
@DisplayName("주어진 경로가 화이트리스트에 등록되어 있는지 확인한다.") | ||
@Test | ||
public void isPathWhitelisted(){ | ||
//given | ||
String requestPath1 = "/api/auth/allowed"; | ||
String requestPath2 = "/actuator/health/not-allowed"; | ||
|
||
//when | ||
boolean result1 = patternProvider.isPathWhitelisted(requestPath1); | ||
boolean result2 = patternProvider.isPathWhitelisted(requestPath2); | ||
|
||
//then | ||
assertThat(result1).isTrue(); | ||
assertThat(result2).isFalse(); | ||
} | ||
|
||
} |