In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use webservice custom annotations to deal with parameter encryption and decryption problems, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Encryption and decryption of webservice custom annotation processing parameters
Webservice was used in the previous project, and I happened to know a little bit about apache's cxf framework, so I adopted cxf to implement the webservice server. There is no technical difficulty in the implementation itself, but in order to ensure security, the project adopts the process of transmission encryption, so most of the request parameters need to be encrypted, and the return parameters also need to be encrypted. The general process is as follows:
Request parameter symmetric encryption + symmetric key asymmetric encryption
Return parameters heap encryption + symmetric key asymmetric encryption
Parameter encryption itself is not complex, but some services do not require encryption, while others do, and encryption utility classes are not universal.
String,datahandler can not be reused, although the essence is to encrypt byte data, but to write a lot of methods, but also need method parameters, check the parameters that need to be encrypted and decrypted, and then find the corresponding type handling method calls.
The logic is clear, but the process implementation is disgusting, and finally a version is implemented, but not to my liking.
If you can get the parameter list in the interceptor, and then find the corresponding decoder to decode, it will be relatively simple. But the difficulty is that if you mark the parameters and specify the decoder of the marked parameters?
When I think of annotations, annotations can mark parameters, and then get annotations through reflection and parse the content.
Originally wanted to mark on the parameters, but a tag is too troublesome, and it is not very convenient to get, just put on the method, using an array to receive.
The problem is solved.
Code implementation
Custom comments:
Import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * @ author webservice request parameter custom annotation * * / @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) public @ interface RequestHanleAnnotation {Class [] handler () default {}; int [] index () default {};}
Parameter decoder interface
Import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * @ author taoyuan * Parameter processor * @ param * / public abstract class ParamHandler {/ / Decoder instance cache public static final Map hanlers=new ConcurrentHashMap (); / / processing method abstract T handle (T t);}
The decoder is implemented. There is no real decoding here, but 123 is added after the parameter.
Public class StringHandler extends ParamHandler {@ Override public String handle (String t) {return t + "123";}}
Use of service method annotations
/ / indicates that the first parameter requires StringHandler to process @ RequestHanleAnnotation (index=0,handler=StringHandler.class) public String test (String test, String test2) throws Exception {System.out.println (test); System.out.println (test2); return "cesshi";}
Interceptor implementation
Import java.io.File;import java.lang.reflect.Method;import java.util.Map;import org.apache.commons.io.FileUtils;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.message.MessageContentsList;import org.apache.cxf.phase.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.apache.log4j.Logger;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject / * * @ author Urban Taoyuan * / public class EcrInInterceptor extends AbstractPhaseInterceptor {private Logger log = Logger.getLogger (ContractLogicImpl.class); public EcrInInterceptor () {/ / call interceptor super (Phase.PRE_INVOKE) before calling the method } @ Override public void handleMessage (SoapMessage msg) throws Fault {/ * get the request ip. You can do the following unified log processing in the interceptor: HttpServletRequest httprequest = (HttpServletRequest) msg.get (AbstractHTTPDestination.HTTP_REQUEST); * * / get the executing method Method method = MsgUtil.getSoapMethod (msg) / / parse the annotation and process the parameter MessageContentsList contentsList = MessageContentsList.getContentsList (msg); MsgUtil.handle (method,contentsList);} @ Override public void handleFault (SoapMessage message) {super.handleFault (message);}
Implementation of annotation parsing tool class
Import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.message.Exchange;import org.apache.cxf.message.MessageContentsList;import org.apache.cxf.service.Service;import org.apache.cxf.service.invoker.MethodDispatcher;import org.apache.cxf.service.model.BindingOperationInfo / * * @ author ll * get the method name * * / public class MsgUtil {/ * * call the method * @ param msg * @ return * / public static Method getSoapMethod (SoapMessage msg) {Exchange exchange = msg.getExchange (); BindingOperationInfo bop = exchange.get (BindingOperationInfo.class) MethodDispatcher md = (MethodDispatcher) exchange.get (Service.class) .get (MethodDispatcher.class.getName ()); Method method= md.getMethod (bop); return method;} public static void handle (Method method, MessageContentsList contentsList) {if (method==null) return; RequestHanleAnnotation reqAnno= method.getAnnotation (RequestHanleAnnotation.class); int [] indexs = reqAnno.index () If (indexs==null | | indexs.length==0) return; Class [] handlers = reqAnno.handler (); try {/ / processor instance ParamHandler handler=null; for (int iIndexes.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.