-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...rg/smartframework/cloud/examples/support/gateway/configure/SmartGatewayConfiguration.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,17 @@ | ||
package org.smartframework.cloud.examples.support.gateway.configure; | ||
|
||
import org.smartframework.cloud.examples.support.gateway.properties.BlackWhiteListProperties; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SmartGatewayConfiguration { | ||
|
||
@Bean | ||
@RefreshScope | ||
public BlackWhiteListProperties blackWhiteListProperties() { | ||
return new BlackWhiteListProperties(); | ||
} | ||
|
||
} |
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
18 changes: 18 additions & 0 deletions
18
.../java/org/smartframework/cloud/examples/support/gateway/exception/BlackListException.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 org.smartframework.cloud.examples.support.gateway.exception; | ||
|
||
import io.github.smart.cloud.exception.BaseException; | ||
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes; | ||
|
||
/** | ||
* 黑名单异常 | ||
* | ||
* @author collin | ||
* @date 2024-03-26 | ||
*/ | ||
public class BlackListException extends BaseException { | ||
|
||
public BlackListException() { | ||
super(GatewayReturnCodes.BLACK_LIST_FORBIDDEN_ACCSS); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
.../java/org/smartframework/cloud/examples/support/gateway/exception/WhiteListException.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 org.smartframework.cloud.examples.support.gateway.exception; | ||
|
||
import io.github.smart.cloud.exception.BaseException; | ||
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes; | ||
|
||
/** | ||
* 白名单异常 | ||
* | ||
* @author collin | ||
* @date 2024-03-26 | ||
*/ | ||
public class WhiteListException extends BaseException { | ||
|
||
public WhiteListException() { | ||
super(GatewayReturnCodes.NOT_IN_WHITE_LIST); | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
...org/smartframework/cloud/examples/support/gateway/filter/access/BlackWhiteListFilter.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,95 @@ | ||
package org.smartframework.cloud.examples.support.gateway.filter.access; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.smartframework.cloud.examples.support.gateway.constants.Order; | ||
import org.smartframework.cloud.examples.support.gateway.exception.BlackListException; | ||
import org.smartframework.cloud.examples.support.gateway.exception.WhiteListException; | ||
import org.smartframework.cloud.examples.support.gateway.properties.BlackWhiteListProperties; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* 黑、白名单校验 | ||
* | ||
* @author collin | ||
* @date 2024-03-26 | ||
*/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class BlackWhiteListFilter implements WebFilter, Ordered { | ||
|
||
private final BlackWhiteListProperties blackWhiteListProperties; | ||
|
||
@Override | ||
public int getOrder() { | ||
return Order.BLACK_WHITE_LIST; | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
String url = exchange.getRequest().getURI().getPath(); | ||
String ipAddress = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress(); | ||
|
||
checkBlackList(url, ipAddress, blackWhiteListProperties.getBlackList()); | ||
checkWhiteList(url, ipAddress, blackWhiteListProperties.getWhiteList()); | ||
|
||
return chain.filter(exchange); | ||
} | ||
|
||
/** | ||
* 检查黑名单 | ||
* | ||
* @param url | ||
* @param ipAddress | ||
* @param blackList | ||
*/ | ||
private void checkBlackList(String url, String ipAddress, Map<String, Set<String>> blackList) { | ||
Set<String> blackIps = blackList.get(url); | ||
if (CollectionUtils.isEmpty(blackIps)) { | ||
return; | ||
} | ||
|
||
for (String blackIp : blackIps) { | ||
if (ipAddress.startsWith(blackIp)) { | ||
throw new BlackListException(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 检查白名单 | ||
* | ||
* @param url | ||
* @param ipAddress | ||
* @param whiteList | ||
*/ | ||
private void checkWhiteList(String url, String ipAddress, Map<String, Set<String>> whiteList) { | ||
Set<String> whiteIps = whiteList.get(url); | ||
if (CollectionUtils.isEmpty(whiteIps)) { | ||
return; | ||
} | ||
|
||
boolean meetWhiteList = false; | ||
for (String whiteIp : whiteIps) { | ||
if (ipAddress.startsWith(whiteIp)) { | ||
meetWhiteList = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!meetWhiteList) { | ||
throw new WhiteListException(); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...rg/smartframework/cloud/examples/support/gateway/properties/BlackWhiteListProperties.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,31 @@ | ||
package org.smartframework.cloud.examples.support.gateway.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* 黑白名单配置 | ||
* | ||
* @author collin | ||
* @date 2024-03-27 | ||
*/ | ||
@Getter | ||
@Setter | ||
@ToString | ||
public class BlackWhiteListProperties { | ||
|
||
/** | ||
* 黑名单<url, 黑名单集合> | ||
*/ | ||
private Map<String, Set<String>> blackList = new LinkedHashMap<>(); | ||
/** | ||
* 白名单<url, 白名单集合> | ||
*/ | ||
private Map<String, Set<String>> whiteList = new LinkedHashMap<>(); | ||
|
||
} |
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
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