前言
想要做網(wǎng)頁Web聊天系統(tǒng)股囊,但是如何對消息特殊字符以及敏感詞進行過濾颗胡、以及處理呢帖旨?
請見下方找田。
敏感消息過濾鏈設計
代碼
不啰嗦直接上源碼:
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;
/**
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-02
*/
public interface Filter {
String doFilter(String message);
}
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
/**
* 過濾器鏈
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-03
*/
public class FilterChain implements Filter {
private List<Filter> filters = new ArrayList<>();
public FilterChain addFilter(Filter filter) {
filters.add(filter);
return this;
}
public String doFilter(String message) {
AtomicReference<String> s = new AtomicReference<>(message);
filters.forEach(f -> s.set(f.doFilter(s.get())));
return s.get();
}
}
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;
/**
* 表情包過濾器
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-03
*/
public class FaceFilter implements Filter {
@Override
public String doFilter(String message) {
return message.replace("@", "^V^").replace("&", "^V^");
}
}
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;
/**
* Html內(nèi)容過濾器
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-02
*/
public class HtmlFilter implements Filter {
@Override
public String doFilter(String message) {
return message.replace("<", "[").replace("/>", "]");
}
}
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;
/**
* 敏感詞過濾器
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-02
*/
public class SensitiveFilter implements Filter {
@Override
public String doFilter(String message) {
return message.replace("王八蛋", "*").replace("軟蛋", "*").replace("滾犢子", "*");
}
}
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain;
import com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter.FilterChain;
import lombok.AllArgsConstructor;
/**
* 信息處理器
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-02
*/
@AllArgsConstructor
public class MessageProsessor {
private String message;
private FilterChain filterChain;
public String process() {
return filterChain.doFilter(message);
}
}
測試類:
package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain;
import com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter.*;
/**
* 敏感信息過濾-》責任鏈設計模式
* @author zhouzhixiang
* @company 叮當快藥科技集團有限公司
* @Date 2019-11-02
*/
public class Main {
public static void main(String[] args) {
String msg = "你好,軟蛋叶雹,@财饥,<script/>,我在北京上班";
FilterChain fc = new FilterChain();
fc.addFilter(new HtmlFilter())
.addFilter(new FaceFilter());
FilterChain fc2 = new FilterChain();
fc2.addFilter(new SensitiveFilter());
fc.addFilter(fc2);
String result = new MessageProsessor(msg, fc).process();
System.out.println(result);
}
}