melonjj 2020-06-04
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
package com.jesse.restful.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Kong on 2020/5/27. */ public class MyInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(request.getRequestURI() + "-准备执行"); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(request.getRequestURI() + "-目标处理成功"); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(request.getRequestURI() + "-响应内容已产生"); } }
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.jesse.restful.interceptor.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/restful/**"/> <mvc:exclude-mapping path="/**.ico"/> <mvc:exclude-mapping path="/**.jpg"/> <mvc:exclude-mapping path="/**.gif"/> <mvc:exclude-mapping path="/**.js"/> <mvc:exclude-mapping path="/**.css"/> <mvc:exclude-mapping path="/resources/**"/> <bean class="com.jesse.restful.interceptor.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name = "console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%thread] %d %level %logger{10} - %msg%n</pattern> </encoder> </appender> <appender name="accessHistoryLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>d:/logs/history.%d.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>[%thread] %d %level %logger{10} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="console"/> </root> <logger name="com.jesse.restful.interceptor.AccessHistoryInterceptor" level="INFO" additivity="false"> <appender-ref ref="accessHistoryLog"/> </logger> </configuration>
package com.jesse.restful.interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Kong on 2020/6/3. */ public class AccessHistoryInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(AccessHistoryInterceptor.class); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { StringBuilder log = new StringBuilder(); log.append(request.getRemoteAddr()); log.append("|"); log.append(request.getRequestURL()); log.append("|"); log.append(request.getHeader("user-agent")); logger.info(log.toString()); return true; } }