写一个Java的白名单过滤
时间: 2024-05-19 08:16:12 浏览: 158
import java.util.ArrayList;
import java.util.List;
public class WhitelistFilter {
private List<String> whitelist;
public WhitelistFilter() {
this.whitelist = new ArrayList<>();
}
public void add(String url) {
this.whitelist.add(url);
}
public boolean isAllowed(String url) {
for (String allowedUrl : this.whitelist) {
if (url.startsWith(allowedUrl)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
WhitelistFilter filter = new WhitelistFilter();
filter.add("http://www.example.com");
filter.add("https://www.example.com");
filter.add("http://subdomain.example.com");
String url1 = "http://www.example.com/index.html";
String url2 = "https://www.example.com/about.html";
String url3 = "http://www.example.org/index.html";
System.out.println(filter.isAllowed(url1)); // true
System.out.println(filter.isAllowed(url2)); // true
System.out.println(filter.isAllowed(url3)); // false
}
}
阅读全文