| | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private SecurityContext getContextByPath() { |
| | | Set<String> urls = AnonTagUtils.getAllAnonymousUrl(applicationContext); |
| | | urls = urls.stream().filter(url -> !url.equals("/")).collect(Collectors.toSet()); |
| | | String regExp = "^(?!" + apiPath + String.join("|" + apiPath, urls) + ").*$"; |
| | | |
| | | // 转义特殊字符(包括{}) |
| | | Function<String, String> escapeRegex = str -> str.replaceAll("([.*+?^${}()|\\[\\]\\\\])", "\\\\$1"); |
| | | |
| | | String escapedApiPath = escapeRegex.apply(apiPath); |
| | | String pattern = urls.stream() |
| | | .map(escapeRegex) // 转义每个URL中的特殊字符 |
| | | .map(url -> escapedApiPath + url) // 拼接API路径 |
| | | .collect(Collectors.joining("|")); |
| | | String regExp = "^(?!" + pattern + ").*$"; |
| | | System.out.println("regExp: " + regExp); |
| | | return SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .operationSelector(o->o.requestMappingPattern() |