Recently, the Android interface written by the company needs to be encrypted to prevent malicious attacks.
1 Most of the interface encryption is carry certificate, we can refer to this big Brother's http://my.oschina.net/jjface/blog/339144
2 This kind of carrying certificate is troublesome, and the code is more cumbersome, the general students are confused, so I think of a simple way, pass two parameters, one is a string type of another is MD5 encryption cipher, write an interceptor/or filter on the server to intercept him, and then do their own corresponding logical processing, Take the string type of the field and add some other things, with MD5 encryption and pass over the ciphertext to do the corresponding comparison.
3 Below is the point, the first time I think of the use of interceptors to intercept URLs, and then do processing, but our service is not just for Android to provide a service interface, there are some internal requests, but sometimes our interface is restful, using Resteasy write, His way of asking is not the same as SPRINGMVC.
@Component
@Path ("Services")
@Produces ({"Application/json"})
@Consumes ({"Application/json"})
This style, but spring's request is this style of
@Controller
@RequestMapping (value = "/". /..")
Interceptor Configuration
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path= "/services/**"/> <bean class= "*. Myinterceptor "></bean> </mvc:interceptor> </mvc:interceptors>
But this configures com.zk.interceptors. Myinterceptor implements the Handlerinterceptor interface, can intercept @requestmapping annotation classes and methods
This can not intercept, and finally think of the way is
<mvc:interceptors> <bean class= "*. Myinterceptor "></bean> </mvc:interceptors>
to intercept all URLs and then manually filter out URLs that do not need to be intercepted on the interceptor itself, processing the interception, but finally The idea is to intercept all URLs, and certainly have an impact on the efficiency of the system,
4 then think of the use of slices, write a section of their own, configure pointcuts, using the section of the @Before ("Execution (* * * * (..))") notification processing. Configure Facets. Specifically, you can see some of this
http://www.cnblogs.com/friends-wf/p/3832876.html
5 The last perfectionist could not tolerate such a thing, feeling that the plane is not perfect, and finally think of the Interceptor intercept URL , filters can also do this, you can first look at the difference between the two
http://www.blogjava.net/hello-yun/archive/2011/11/21/364469.html
When writing a filter, Need to get the parameters of request, GET request Servletrequest.getparameter (""), directly write this can be obtained, but the most troublesome is the POST request
because the POST request some data will be written to the stream, You need to go to the stream to get the data,
/**
* Get data from the request stream
* @param request
* @return
* @throws Exception
*/
Public String getDate (HttpServletRequest request) throws exception{
StringBuilder sb = new StringBuilder ();
InputStream inputstream = null;
BufferedReader reader = null;
try {
InputStream = Request.getinputstream ();
reader = new BufferedReader (new InputStreamReader (InputStream, Charset.forname ("UTF-8"));
String line = "";
while (line = Reader.readline ()) = null) {
Sb.append (line);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (InputStream! = null) {
try {
Inputstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (reader! = null) {
try {
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
return sb.tostring ();
}
This data can also be obtained, to do this I think it is possible, but a measured data is obtained, but in the execution, because you are in the filter to get the data in the stream, the data in the stream can only be read once,
When you're done, you'll need to write back the data in the stream before you can read it in the business process.
Package com.imxiaomai.cb.wms.web.filter;
Import Java.io.BufferedReader;
Import Java.io.ByteArrayInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.nio.charset.Charset;
Import Javax.servlet.ServletInputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletRequestWrapper;
/**
* User:liqiang
*/
public class Bodyreaderhttpservletrequestwrapper extends Httpservletrequestwrapper {
Private final byte[] body;
Public Bodyreaderhttpservletrequestwrapper (httpservletrequest request,string data) throws IOException {
Super (Request);
BODY = Data.getbytes (Charset.forname ("UTF-8"));
}
@Override
Public BufferedReader Getreader () throws IOException {
return new BufferedReader (New InputStreamReader (getInputStream ()));
}
@Override
Public ServletInputStream getInputStream () throws IOException {
Final Bytearrayinputstream Bais = new Bytearrayinputstream (body);
return new ServletInputStream () {
@Override
public int read () throws IOException {
return Bais.read ();
}
public Boolean isfinished () {
return false;
}
public Boolean IsReady () {
return false;
}
};
}
}
Specifically, you can look at this http://my.oschina.net/vernon/blog/363693.
Android Interface Encryption