代理模式,数组,迭代器|一点小细节

要点以及遇到的问题:
1.代理模式增强功能
2.request.getParameterMap,底层源码给map加锁.map得值不可修改
3.解决:值放置到数组中时,遍历数组可修改.
注意:不能随便使用迭代器遍历修改.
引用数组修改的是内存地址中的实际参数,而for循环里面的局部变量为实际参数的副本,
修改其值不会修改实际的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//过滤器
package com.fzq.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;

@WebFilter("/*")
public class SensitiveWordsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

//代理过滤
ServletRequest proxyObj = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//增强getParameter方法
//判断是否是getParameter方法
if (method.getName().equals("getParameter")) {
//增强返回值
//获取返回值
String value = (String) method.invoke(req, args);
if (value != null) {
for (String s : list) {
if (value.contains(s)) {
value = value.replaceAll(s, "***");
}
}
}
return value;
} else if (method.getName().equals("getParameterValues")) {
String[] value = (String[]) method.invoke(req, args);
for (int i = 0; i < value.length; i++) {
if (value[i] != null) {
for (String s : list) {
if (value[i].contains(s)) {
value[i] = value[i].replaceAll(s, "***");
}
}
}

}
return value;

} else if(method.getName().equals("getParameterMap")){
Map<String,String[]> map = (Map<String, String[]>) method.invoke(req, args);

if (map!=null){
for (Map.Entry<String, String[]> entry : map.entrySet()) {

/**
* 不能直接map.put使用修改覆盖的方法,map加锁无法修改
*/
String[] values = entry.getValue();
/**
* emmmm呢
* 不能使用迭代器!!
* 迭代器新创建一个局部变量修改了值便销毁了
* 修改value数组,引用传递,修改的是内存地址里面的值
*/
for (int i = 0; i < values.length; i++) {
for (String sensitiveWord : list) {
if (values[i].contains(sensitiveWord)){
values[i] = values[i].replaceAll(sensitiveWord,"小可爱");
}
}
}

}
}

/************************************************************
* o98k
Set<String> set = map.keySet();
//获取key
if(map!=null){
for (String key : set) {
//value
String[] value = map.get(key);
//敏感词判断
for (int i = 0; i < value.length; i++) {
for (String sensitiveWords : list) {
if (value[i].contains(sensitiveWords)){
value[i] = value[i].replaceAll(sensitiveWords,"小可爱");
}
}
}
}
}
***************************************************/
/*
foolish 2.0
Iterator<Map.Entry<String, String[]>> it = map.entrySet().iterator();

while(it.hasNext()){
String[] value = it.next().getValue();
for (int i = 0; i < value.length; i++) {
for (String s : list) {
if (value[i].contains(s)){
value[i] = value[i].replaceAll(s,"小可爱");
}
}
}
*/
/*
//foolish 1.0
if(value[0]!=null){
for (String s : list) {
if(value[0].contains(s)){
value[0]=value[0].replaceAll(s,"***");
}
}
}*/


return map;
}

return method.invoke(req, args);
}
});


//放行
chain.doFilter(proxyObj,resp);
}

private List<String> list = new ArrayList<String>();

public void init(FilterConfig config) throws ServletException {
//获取文件真实路径

//读取文件
try {
ServletContext servletContext = config.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");
BufferedReader br = new BufferedReader(new FileReader(realPath));
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}

br.close();
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void destroy() {

}


}

//test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.fzq.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

/* String name = request.getParameter("name");
String msg = request.getParameter("msg");
System.out.println(name+":"+msg);*/

//map
Map<String, String[]> map = request.getParameterMap();
for (Map.Entry<String, String[]> entry : map.entrySet()) {
String name = entry.getKey();
String[] msg = entry.getValue();
System.out.println(name+":"+msg[0]);
}


/*

String name = request.getParameterValues("name")[0];
String msg = request.getParameterValues("msg")[0];
System.out.println(name+":"+msg);
*/



}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}