项目背景: 2002年的某保险老项目,项目是部署了多个服务器,每个服务器有2到多个服务(每个服务的日志对应一个日志文件),外部对接是通过F5分发到随机服务器上来进行访问,正式出现问题或者看一些问题就需要一个服务器一个服务器去找日志(运气不好直接看遍所有的服务器日志文件),所以考虑做个接口调用时调用的IP和端口的记录,来方便查找问题日志

web.xml增加拦截器是最简单的方法,不过leader害怕影响了整个项目不让修改,所以只能改自己涉及的接口,

接口是通过axis2的aar包方式发布(WEB-INFO下servers文件夹下所有aar包),这里全局axis2.xml也没有直接加载的是jar包里面默认的配置,leader这里也不让加全局的文件,也是担心影响整个项目(吐槽下,太事),所以只能通过moudle.xml来配置,然后修改自己接口的aar包里面的services,xml配置文件来拦截获取了

百度了老半天,终于解决了(这里参考): https://blog.csdn.net/u013313550/article/details/53285011

先来程序代码

  

package com.axis2.module;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

/**该类虽然没有实现实际的功能,但是必须要存在的,因为模块的初始化与销毁都必须在该类中完成,*/
public class RMPhaseModule implements Module{ /**初始化模块类 */
public void init(ConfigurationContext paramConfigurationContext,
AxisModule paramAxisModule) throws AxisFault {
System.out.println("axis2[RMPhaseModule] 模块初始化完成!!!"); } public void engageNotify(AxisDescription paramAxisDescription)
throws AxisFault { } public boolean canSupportAssertion(Assertion paramAssertion) {
return false;
} public void applyPolicy(Policy paramPolicy,
AxisDescription paramAxisDescription) throws AxisFault { } public void shutdown(ConfigurationContext paramConfigurationContext)
throws AxisFault { System.out.println("axis2[RMPhaseModule] 模块已停止执行。。。");
} }

  

package com.axis2.handler;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.transport.http.HTTPConstants; import com.axis2.ServerInfo;

/**该类拦截请求,将自己需要的信息保存到线程变量里面,该类的调用在modules.xml里面配置*/
public class ServerInfoHandler extends AbstractHandler implements Handler{ public InvocationResponse invoke(MessageContext paramMessageContext)
throws AxisFault {
HttpServletRequest request = (HttpServletRequest)paramMessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
System.out.println("ServerInfoHandler invoke() 初始化服务的信息到ThreadLocal");
ServerInfo serverInfo = new ServerInfo();
serverInfo.setIp(ServerInfo.getIpAddress(request, true));
serverInfo.setPort(request.getServerPort());
ServerInfo.local_thread.set(serverInfo);
return InvocationResponse.CONTINUE;
} }

  

package com.axis2.handler;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler; import com.axis2.ServerInfo;

/** 程序处理完后,清楚线程变量的值 */
public class ServerInfoRevokeHandler extends AbstractHandler implements Handler{ public InvocationResponse invoke(MessageContext paramMessageContext)
throws AxisFault {
ServerInfo.local_thread.remove();
System.out.println("销毁ThreadLocal-ServerInfo");
return InvocationResponse.CONTINUE;
} }

  

package com.axis2;

import java.net.InetAddress;
import java.net.UnknownHostException; import javax.servlet.http.HttpServletRequest; /** 简单的pojo类,来保存信息 */
public class ServerInfo { public static final ThreadLocal<ServerInfo> local_thread = new ThreadLocal<ServerInfo>(); private String ip; private int port; public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} /**
* 获取Ip地址
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request,boolean isLocal) {
String ipAddress = null;
try {
if(isLocal){
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return inet.getHostAddress();
}
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress = "";
}
// ipAddress = this.getRequest().getRemoteAddr(); return ipAddress;
} }

  这里涉及的程序代码就没有了,当时让我头疼的是配置这块的问题,不是项目启不来,就是module类加载不了,各种报错

配置


  编写modules.xml, RMPhase是默认axis2.xml配置里面预留出来的,可以直接用,这样不用重新整axis2.xml(这里是leader最忌讳的,其实是瞎担心)

<?xml version="1.0" encoding="UTF-8"?>
<module name="serverInfo" class="com.axis2.module.RMPhaseModule">
<InFlow>
<handler name="InFlowLogHandler" class="com.axis2.handler.ServerInfoHandler">
<order phase="RMPhase"/>
</handler>
</InFlow>
<OutFlow>
<handler name="OutFlowLogHandler" class="com.axis2.handler.ServerInfoRevokeHandler">
<order phase="RMPhase"/>
</handler>
</OutFlow>
<!-- <OutFaultFlow>
<handler name="FaultOutFlowLogHandler" class="com.test.module.handler.LoggingHandler">
<order phase="RMPhase"/>
</handler>
</OutFaultFlow>
<InFaultFlow>
<handler name="FaultInFlowLogHandler" class="com.test.module.handler.LoggingHandler">
<order phase="RMPhase"/>
</handler>
</InFaultFlow> -->
</module>

 自己对应接口的***.aar包里面的services.xml添加module

<service name="YTBWasForwardXmlProxy">
<description>YTBWasForwardXmlProxy</description>
<parameter name="ServiceClass">
com.sinosoft.wasforwardxml.project.padcheck.services.YTBWasForwardXmlProxy
</parameter>
<!-- 引入这个module-->
<module ref="serverInfo"/>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>

aar包是之前已经发布的,这里只需要重新修改下xml文件就行,我是没解压的情况下直接winrar里面修改的

剩下就是发布mar包了(这里要感谢参考文章的大神了)

目录结构

test(随便一个文件名都行,路径也无所谓)

------com
------com/axis2
------com/axis2/ServerInfo.class
------com/axis2/module
------com/axis2/module/RMPhaseModule
------com/axis2/handler
------com/axis2/handler/ServerInfoHandler
------com/axis2/handler/ServerInfoRevokeHandler
------META-INF
------META-INF/modules.xml

  

黑窗口(DOS命令进入test文件夹),打包文件名为serverInfo,jar,然后修改.jar为.mar

将serverInfo.jar修改为serverInfo.mar.复制到项目目录WEB-INFO下的modules文件夹下即可(无此文件夹新建)

这里注意命令 ( jar cvf serverInfo.jar .) 最后有个.别漏了

程序里面使用相应信息的时候直接 获取即可

弊端是如果其他arr也需要修改相应的配置文件,这个项目里面有相当多的aar,这也是个繁琐的过程,

最后leader针对业务最后只获取了服务器的IP(程序直接可以获取,无需请求对象),没有要其他的信息

我也只更新了自己的接口,方便自己查找日志,只更新了一个aar

axis2添加拦截器的更多相关文章

  1. spring boot 添加拦截器

    构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...

  2. javaweb添加拦截器

    js请求后台代码添加拦截器: package com.ctzj.biz.isale.deploy.controller; import java.io.IOException; import java ...

  3. (七)CXF添加拦截器

    今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...

  4. 使用CXF为webservice添加拦截器

      拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Servi ...

  5. SpringBoot如何添加拦截器

    在web开发的过程中,为了实现登录权限验证,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢? 步骤如下: 1.继承Web ...

  6. CXF添加拦截器和自定义拦截器

    前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...

  7. (八)CXF之用spring添加拦截器

    一.案例 本章案例是基于CXF之自定义拦截器基础之上改造的,目的是在服务端中用spring添加拦截器 配置web.xml <?xml version="1.0" encodi ...

  8. (五)CXF之添加拦截器

    一.需求分析 webService中的拦截器类似于servlet的Filter过滤器.一般用于调用服务前后先调用拦截器的方法. 二.案例 本章案例是基于上一章节的基础上添加拦截器的 2.1 服务端添加 ...

  9. 在Java后端如何添加拦截器

    在安全编码规范中,在Java后端controller层接口需要对调用者的身份进行确认,以防非法用户进行访问.若是在controller层的每个接口处都添加逻辑判断,那么代码重复度高,并且费力费时.此时 ...

  10. Spring Boot 2.X 如何添加拦截器?

    最近使用SpringBoot2.X搭建了一个项目,大部分接口都需要做登录校验,所以打算使用注解+拦截器来实现,在此记录下实现过程. 一.实现原理 1. 自定义一个注解@NeedLogin,如果接口需要 ...

随机推荐

  1. 松灵机器人scout mini小车 自主导航(2)——仿真指南

    松灵机器人Scout mini小车仿真指南 之前介绍了如何通过CAN TO USB串口实现用键盘控制小车移动.但是一直用小车测试缺乏安全性.而松灵官方贴心的为我们准备了gazebo仿真环境,提供了完整 ...

  2. 使用requests库实现http请求

    1.发送请求 import requests url = 'http://www.tipdm.com/tipdm/index.html' rqq = requests.get(url) In [ ]: ...

  3. SQL Server 验证某栏位是否存在某字符串(CHARINDEX)

    SELECT * FROM LiuJun_PKqitchqi WHERE CHARINDEX('230527Z3258',qr_code) > 0

  4. [oeasy]python005_退出游乐场_重启游乐场_系统态shell_应用态_quit

    退出终端_重启游乐场_shell_quit Python 回忆 上次 了解了 python 进入了 python 游乐场   在游乐场 可以做 简单的计算 还可以做 乘方运算   数字特别大之后 游乐 ...

  5. 阅读翻译Mathematics for Machine Learning之2.7 Linear Mappings

    阅读翻译Mathematics for Machine Learning之2.7 Linear Mappings 关于: 首次发表日期:2024-07-23 Mathematics for Machi ...

  6. VirtualBox扩容CentOS-7虚拟机磁盘

    1.背景描述 如上图所示,根路径"/"所在的文件系统已没有可用的磁盘空间,需要扩容磁盘. df -h 2.VirtualBox操作 2.1.查看当前虚拟磁盘的大小 如上图所示,点击 ...

  7. 暑假自学Java进度总结04

    一.今日所学: 1.下载并使用idea开发工具 1>了解idea的发展历史 2>尝试用idea编写代码 3>学习idea中的项目和模块操作 2.学习赋值运算符 加后赋值:" ...

  8. 如何理解计算机类论文、机器学习论文、人工智能AI论文中的“soft”和“hard”呢?

    如何理解计算机类论文.机器学习论文.人工智能AI论文中的"soft"和"hard"呢? 最近在看论文中总看到带有"soft"和"h ...

  9. (续) python 中 ctypes 的使用尝试

    内容接前文: https://www.cnblogs.com/devilmaycry812839668/p/15032493.html ================================ ...

  10. pyglet.gl.ContextException: Could not create GL context

    参考: https://www.saoniuhuo.com/question/detail-2725960.html ========================================= ...