springMVC系列(五)——springMVC拦截器实现接口访问日志
2017-05-16 18:43
489 查看
业务需求
记录访问接口的机器信息、ip地址、时间等数据并存储在数据库表中,要求对原代码没有入侵性,机器信息放在请求的中。
1.接口访问日志表mb_accesslog
记录访问接口的机器信息、ip地址、时间等数据并存储在数据库表中,要求对原代码没有入侵性,机器信息放在请求的中。
1.接口访问日志表mb_accesslog
CREATE TABLE`mb_accesslog`( `id` int(11) NOTNULLAUTO_INCREMENT, `ip` varchar(20) NOTNULLCOMMENT 'IP', `code`varchar(20) NOT NULL COMMENT 'Code', `mac` varchar(20) NOTNULLCOMMENT 'Mac', `account`varchar(20) NOT NULL COMMENT 'Account', `model`varchar(50) DEFAULT NULL COMMENT '机型', `accesstime`datetimeNOTNULLCOMMENT '访问时间', `resource`varchar(100) NOT NULL COMMENT '访问资源', PRIMARYKEY(`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1261 DEFAULT CHARSET=utf8;2.拦截器AccesslogInterceptor,拦截器类必须实现HandlerInterceptorAdapter类,preHandle方法是拦截在访问开始前。
@Component public classAccesslogInterceptor extends HandlerInterceptorAdapter { @Autowired private AccesslogService accesslogService; public booleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Stringip = request.getRemoteAddr(); Stringcode= (String)request.getAttribute("code"); Stringmac= (String)request.getAttribute("mac"); Stringaccount= (String)request.getAttribute("account"); Dateaccesstime= newDate(); Stringresource= request.getRequestURI().split("/")[2]; Accesslogaccesslog= newAccesslog(ip, code, mac, account, null, accesstime, resource); accesslogService.insertOne(accesslog); return true; } }3.配置拦截器
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/bls/*"/><!-- 如果不配置或/*,将拦截所有的Controller --> <bean class="com.yoostar.support.interceptor.AccesslogInterceptor"/> </mvc:interceptor> </mvc:interceptors>4.编写测试类,通过get访问
@Test public voidtestPost() throwsIOException { Stringurl= "http://localhost:8080/study_ssmvc/bls/test.m3u8"; Stringparam= "code=1&mac=2&account=12"; URLrealUrl= newURL(url); URLConnectionuc = realUrl.openConnection(); // 发送POST请求必须设置如下两行 uc.setDoOutput(true); uc.setDoInput(true); OutputStreamos = uc.getOutputStream(); PrintWriterout= newPrintWriter(os); out.print(param); out.flush(); // 定义BufferedReader输入流来读取URL的响应 BufferedReaderin = new BufferedReader( new InputStreamReader(uc.getInputStream())); //这里才开始访问 String line; String result= ""; while((line= in.readLine()) != null) { result += line + "\n"; } System.out.println(result); }
相关文章推荐
- springMVC系列(六)——拦截器实现自定义注解接口访问限制
- springMVC笔记系列(23)——拦截器及其在乱码问题和登录问题的应用实现
- 基于springMVC拦截器实现操作日志统计
- 实现IHttpModule接口获取Session来实现页面访问日志功
- Springmvc拦截器实现网站非登录不能访问。
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- springmvc实现jsonp跨域接口访问
- SpringMVC自定义注解和拦截器实现控制器访问次数限制
- 实现IHttpModule接口获取Session来实现页面访问日志功能。
- 实现IHttpModule接口获取Session来实现页面访问日志功能。
- SpringMVC接口权限拦截器的实现
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- springMvc拦截器实现操作日志的自动持久层
- Spring.net抛砖引玉系列(二)用接口来实现HelloWorld
- 【软件测试自动化-QTP系列讲座 34】== 访问对象自身接口的特殊渠道 ==
- C#中显/隐式实现接口及其访问方法
- DotNet企业级架构实战之5——基于接口的访问层实现
- NHibernate开源框架Cuyahoga学习之数据访问接口的实现