Java Code Examples for com.alibaba.csp.sentinel.slots.block.RuleConstant#AUTHORITY_WHITE

The following examples show how to use com.alibaba.csp.sentinel.slots.block.RuleConstant#AUTHORITY_WHITE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthorityRuleController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private <R> Result<R> checkEntityInternal(AuthorityRuleEntity entity) {
    if (entity == null) {
        return Result.ofFail(-1, "bad rule body");
    }
    if (StringUtil.isBlank(entity.getApp())) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(entity.getIp())) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (entity.getPort() == null || entity.getPort() <= 0) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (entity.getRule() == null) {
        return Result.ofFail(-1, "rule can't be null");
    }
    if (StringUtil.isBlank(entity.getResource())) {
        return Result.ofFail(-1, "resource name cannot be null or empty");
    }
    if (StringUtil.isBlank(entity.getLimitApp())) {
        return Result.ofFail(-1, "limitApp should be valid");
    }
    if (entity.getStrategy() != RuleConstant.AUTHORITY_WHITE
        && entity.getStrategy() != RuleConstant.AUTHORITY_BLACK) {
        return Result.ofFail(-1, "Unknown strategy (must be blacklist or whitelist)");
    }
    return null;
}
 
Example 2
Source File: AuthorityRuleChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
static boolean passCheck(AuthorityRule rule, Context context) {
    String requester = context.getOrigin();

    // Empty origin or empty limitApp will pass.
    if (StringUtil.isEmpty(requester) || StringUtil.isEmpty(rule.getLimitApp())) {
        return true;
    }

    // Do exact match with origin name.
    int pos = rule.getLimitApp().indexOf(requester);
    boolean contain = pos > -1;

    if (contain) {
        boolean exactlyMatch = false;
        String[] appArray = rule.getLimitApp().split(",");
        for (String app : appArray) {
            if (requester.equals(app)) {
                exactlyMatch = true;
                break;
            }
        }

        contain = exactlyMatch;
    }

    int strategy = rule.getStrategy();
    if (strategy == RuleConstant.AUTHORITY_BLACK && contain) {
        return false;
    }

    if (strategy == RuleConstant.AUTHORITY_WHITE && !contain) {
        return false;
    }

    return true;
}
 
Example 3
Source File: AuthorityRuleController.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private <R> Result<R> checkEntityInternal(AuthorityRuleEntity entity) {
    if (entity == null) {
        return Result.ofFail(-1, "bad rule body");
    }
    if (StringUtil.isBlank(entity.getApp())) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(entity.getIp())) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (entity.getPort() == null || entity.getPort() <= 0) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (entity.getRule() == null) {
        return Result.ofFail(-1, "rule can't be null");
    }
    if (StringUtil.isBlank(entity.getResource())) {
        return Result.ofFail(-1, "resource name cannot be null or empty");
    }
    if (StringUtil.isBlank(entity.getLimitApp())) {
        return Result.ofFail(-1, "limitApp should be valid");
    }
    if (entity.getStrategy() != RuleConstant.AUTHORITY_WHITE
        && entity.getStrategy() != RuleConstant.AUTHORITY_BLACK) {
        return Result.ofFail(-1, "Unknown strategy (must be blacklist or whitelist)");
    }
    return null;
}
 
Example 4
Source File: AuthorityRuleChecker.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
static boolean passCheck(AuthorityRule rule, Context context) {
    String requester = context.getOrigin();

    // Empty origin or empty limitApp will pass.
    if (StringUtil.isEmpty(requester) || StringUtil.isEmpty(rule.getLimitApp())) {
        return true;
    }

    // Do exact match with origin name.
    int pos = rule.getLimitApp().indexOf(requester);
    boolean contain = pos > -1;

    if (contain) {
        boolean exactlyMatch = false;
        String[] appArray = rule.getLimitApp().split(",");
        for (String app : appArray) {
            if (requester.equals(app)) {
                exactlyMatch = true;
                break;
            }
        }

        contain = exactlyMatch;
    }

    int strategy = rule.getStrategy();
    if (strategy == RuleConstant.AUTHORITY_BLACK && contain) {
        return false;
    }

    if (strategy == RuleConstant.AUTHORITY_WHITE && !contain) {
        return false;
    }

    return true;
}