Commit 03284b4a91e8c656c986c210386b89f904a16994
1 parent
48c9abe3
选择历史版本,在线路里查看历史版本修改当前和待更新版本
Showing
1 changed file
with
103 additions
and
0 deletions
src/main/java/com/bsth/filter/AccessLogFilter.java
| 1 | +<<<<<<< HEAD | |
| 1 | 2 | package com.bsth.filter; |
| 2 | 3 | |
| 3 | 4 | import com.alibaba.fastjson.JSON; |
| ... | ... | @@ -98,3 +99,105 @@ public class AccessLogFilter extends BaseFilter { |
| 98 | 99 | return "[" + msg.toString() + "]"; |
| 99 | 100 | } |
| 100 | 101 | } |
| 102 | +======= | |
| 103 | +package com.bsth.filter; | |
| 104 | + | |
| 105 | +import com.alibaba.fastjson.JSON; | |
| 106 | +import com.bsth.entity.sys.SysUser; | |
| 107 | +import com.bsth.security.util.SecurityUtils; | |
| 108 | +import com.bsth.util.IpUtils; | |
| 109 | +import com.google.common.collect.Lists; | |
| 110 | +import com.google.common.collect.Maps; | |
| 111 | +import org.slf4j.Logger; | |
| 112 | +import org.slf4j.LoggerFactory; | |
| 113 | +import org.springframework.stereotype.Component; | |
| 114 | + | |
| 115 | +import javax.servlet.FilterChain; | |
| 116 | +import javax.servlet.ServletException; | |
| 117 | +import javax.servlet.http.HttpServletRequest; | |
| 118 | +import javax.servlet.http.HttpServletResponse; | |
| 119 | +import java.io.IOException; | |
| 120 | +import java.util.Enumeration; | |
| 121 | +import java.util.List; | |
| 122 | +import java.util.Map; | |
| 123 | + | |
| 124 | +/** | |
| 125 | + * | |
| 126 | + * @ClassName: AccessLogFilter | |
| 127 | + * @Description: TODO(记录访问日志) | |
| 128 | + * @author PanZhao | |
| 129 | + * @date 2016年3月17日 下午4:28:31 | |
| 130 | + * | |
| 131 | + */ | |
| 132 | +@Component | |
| 133 | +public class AccessLogFilter extends BaseFilter { | |
| 134 | + | |
| 135 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 136 | + | |
| 137 | + @Override | |
| 138 | + public void doFilter(HttpServletRequest request, | |
| 139 | + HttpServletResponse response, FilterChain chain) | |
| 140 | + throws IOException, ServletException { | |
| 141 | + | |
| 142 | + SysUser user = null; | |
| 143 | + if (request.getParameter("token") != null) { | |
| 144 | + user = new SysUser(); | |
| 145 | + user.setUserName("admin"); | |
| 146 | + } else { | |
| 147 | + user = SecurityUtils.getCurrentUser(); | |
| 148 | + } | |
| 149 | + String username = user.getUserName(); | |
| 150 | + String name = user.getName(); | |
| 151 | + String jsessionId = request.getRequestedSessionId(); | |
| 152 | + String ip = IpUtils.getIpAddr(request); | |
| 153 | + String userAgent = request.getHeader("User-Agent"); | |
| 154 | + String url = request.getRequestURI(); | |
| 155 | + String params = getParams(request); | |
| 156 | + String headers = getHeaders(request); | |
| 157 | + String method = request.getMethod(); | |
| 158 | + | |
| 159 | + StringBuilder s = new StringBuilder(); | |
| 160 | + s.append(getBlock(username + " -" + name)); | |
| 161 | + s.append(getBlock(jsessionId)); | |
| 162 | + s.append(getBlock(ip)); | |
| 163 | + s.append(getBlock(userAgent)); | |
| 164 | + s.append(getBlock(url)); | |
| 165 | + s.append(getBlock(method)); | |
| 166 | + s.append(getBlock(params)); | |
| 167 | + s.append(getBlock(headers)); | |
| 168 | + s.append(getBlock(request.getHeader("Referer"))); | |
| 169 | + | |
| 170 | + long now = System.currentTimeMillis(); | |
| 171 | + chain.doFilter(request, response); | |
| 172 | + s.append("<cost time:").append(System.currentTimeMillis() - now).append(">"); | |
| 173 | + logger.info(s.toString()); | |
| 174 | + } | |
| 175 | + | |
| 176 | + private static String getParams(HttpServletRequest request) { | |
| 177 | + Map<String, String[]> params = request.getParameterMap(); | |
| 178 | + return JSON.toJSONString(params); | |
| 179 | + } | |
| 180 | + | |
| 181 | + private static String getHeaders(HttpServletRequest request) { | |
| 182 | + Map<String, List<String>> headers = Maps.newHashMap(); | |
| 183 | + Enumeration<String> namesEnumeration = request.getHeaderNames(); | |
| 184 | + while (namesEnumeration.hasMoreElements()) { | |
| 185 | + String name = namesEnumeration.nextElement(); | |
| 186 | + Enumeration<String> valueEnumeration = request.getHeaders(name); | |
| 187 | + List<String> values = Lists.newArrayList(); | |
| 188 | + while (valueEnumeration.hasMoreElements()) { | |
| 189 | + values.add(valueEnumeration.nextElement()); | |
| 190 | + } | |
| 191 | + headers.put(name, values); | |
| 192 | + } | |
| 193 | + return JSON.toJSONString(headers); | |
| 194 | + } | |
| 195 | + | |
| 196 | + public static String getBlock(Object msg) { | |
| 197 | + if (msg == null) { | |
| 198 | + msg = ""; | |
| 199 | + } | |
| 200 | + return "[" + msg.toString() + "]"; | |
| 201 | + } | |
| 202 | +} | |
| 203 | +>>>>>>> 2021.05.11 历史版本 选择版本 查看历史版本修改当前版本和待更新版本 | ... | ... |