Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the encryption and Compression of APP HTTP Interface message

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will introduce to you how to encrypt and compress APP HTTP interface messages. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

When doing APP products, the whole business logic is usually placed on the server, and most of the clients are only used for presentation. In the process of interaction between the client and the server, the security of messages and the saving of traffic are relatively important. This article shows how to encrypt a message through 3DES and compress it through the gzip that comes with Nginx.

One: encryption rules and procedures

2: enable gzip configuration for nginx

Gzip on

Gzip_min_length 1k

Gzip_buffers 4 16k

Gzip_http_version 1.1

Gzip_comp_level 5

Gzip_types application/json

Gzip_vary on

Gzip

Syntax: gzip on | off

Default value: gzip off

Scope: http, server, location, if (x) location

Turn the gzip module on or off

Gzip_buffers

Syntax: gzip_buffers number size

Default value: gzip_buffers 4 4k/8k

Scope: http, server, location

Set up the system to get several units of cache to store the compressed result data stream of gzip. For example, 44k represents four times the amount of memory requested in 4k units according to the original data size. 48k represents 4 times the memory requested in 8k units according to the original data size in 8k units.

If it is not set, the default value is to request the same amount of memory space as the original data to store the gzip compression results.

Gzip_comp_level

Syntax: gzip_comp_level 1.. 9

Default value: gzip_comp_level 1

Scope: http, server, location

Gzip compression ratio, 1 compression ratio minimum processing speed is the fastest, 9 compression ratio is the largest but processing is the slowest (transmission is fast but consumes cpu).

Gzip_min_length

Syntax: gzip_min_length length

Default value: gzip_min_length 0

Scope: http, server, location

Sets the minimum number of bytes of a page that is allowed to be compressed, which is obtained from the Content-Length in the header header.

The default value is 0, no matter how much the page is compressed.

It is recommended to set the number of bytes greater than 1k. Less than 1k may increase the pressure. Namely: gzip_min_length 1024

Gzip_http_version

Syntax: gzip_http_version 1.0 | 1.1

Default value: gzip_http_version 1.1

Scope: http, server, location

Identify the protocol version of the http. Since some early browsers or http clients may not support gzip self-decompression, users will see garbled code, so it is necessary to make some judgments. Note: the 21st century has come, and now, except for things like Baidu spiders, which do not support self-decompression (Baidu is SX, I will not say it), 99.99% of browsers basically support gzip decompression, so you don't have to set this value and keep the system default.

Gzip_proxied

Syntax: gzip_proxied [off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any].

Default value: gzip_proxied off

Scope: http, server, location

When Nginx is enabled as a reverse proxy, the result returned by the back-end server is enabled or disabled. The premise of the match is that the back-end server must return a header header containing "Via".

Off-turn off compression of all agent result data

Expired-enables compression if the header header contains "Expires" header information

No-cache-enables compression if the header header contains "Cache-Control:no-cache" header information

No-store-enables compression if the header header contains "Cache-Control:no-store" header information

Private-enables compression if the header header contains "Cache-Control:private" header information

No_last_modified-enables compression if the header header does not contain "Last-Modified" header information

No_etag-enables compression if the header header does not contain "ETag" header information

Auth-enables compression if the header header contains "Authorization" header information

Any-unconditionally enable compression

Gzip_types

Syntax: gzip_types mime-type [mime-type...]

Default value: gzip_types text/html

Scope: http, server, location

Third, client Http request and return result parsing

Public class HttpClientUtilTest {

Private static Logger logger = Logger.getLogger (HttpClientUtil.class)

Private static HttpClient httpClient = null

Private static HttpPost postMethod = null

Private static HttpGet getMethod = null

Private static long startTime = 0L

Private static long endTime = 0L

Private static ObjectMapper jsonMapper

Static {

JsonMapper = new ObjectMapper ()

JsonMapper.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"))

JsonMapper.configure (Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)

JsonMapper.configure (Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)

}

/ * *

* @ description sends a json request and returns the result

* @ param url

* request address

* @ param jsonRequest

* request parameters

* @ return jsonResponse returns the result

* @ throws

, /

Public static String sendJsonRequest (String url, String jsonRequest)

Throws Exception {

Logger.info ("request URL:" + url)

Logger.info ("request parameters:" + jsonRequest)

String jsonResponse = null

HttpClient = new DefaultHttpClient ()

PostMethod = new HttpPost (url)

StringEntity entity = new StringEntity (jsonRequest, "UTF-8")

Entity.setContentEncoding (new BasicHeader (HTTP.CONTENT_ENCODING)

Consts.UTF_8.toString ())

Entity.setContentType (new BasicHeader (HTTP.CONTENT_TYPE)

"application/json;charset=UTF-8"))

PostMethod.setEntity (entity)

PostMethod.getParams (). SetParameter

CoreProtocolPNames.USER_AGENT

System.getProperty ("os.name") + "(version"

+ System.getProperty ("os.version") + ""

+ System.getProperty ("os.arch") + ")"

+ System.getProperty ("user.language"))

/ / add header information to tell the server that Response can be compressed by GZip

PostMethod.setHeader ("Accept-Encoding", "gzip, deflate")

/ / start the request

StartTime = System.currentTimeMillis ()

HttpResponse response = httpClient.execute (postMethod)

Logger.info ("status:" + response.getStatusLine () .getStatusCode ())

/ / read the request result

If (HttpStatus.SC_OK = = response.getStatusLine () .getStatusCode ()) {

HttpEntity httpEntity = response.getEntity ()

/ / decrypt the returned result compressed by gzip

If (response.getFirstHeader ("Content-Encoding")! = null & & response.getFirstHeader ("Content-Encoding"). GetValue (). ToLowerCase (). IndexOf ("gzip") >-1)

{

JsonResponse = dealGzipResponse (response)

}

Else {

JsonResponse = EntityUtils.toString (httpEntity)

}

}

EndTime = System.currentTimeMillis ()

Logger.info ("return result size:" + jsonResponse.length ())

Logger.info ("return result:" + Des3.decode (jsonResponse, "tUzzx% Ulqz3V2B4roomdQnt9iL")

Logger.info ("time spent on this request:" + (endTime-startTime) + "ms")

Return jsonResponse

}

/ * *

* @ description returned results from parsing gzip

* @ param

* @ return

* @ throws

, /

Public static String dealGzipResponse (HttpResponse response) throws IOException

{

Logger.info ("= result returned by parsing gzip =")

InputStream is = response.getEntity () .getContent ()

GZIPInputStream gzin = new GZIPInputStream (is)

InputStreamReader isr = new InputStreamReader (gzin, "UTF-8")

Java.io.BufferedReader br = new java.io.BufferedReader (isr)

StringBuffer sb = new StringBuffer ()

String tempbf

While ((tempbf = br.readLine ())! = null) {

Sb.append (tempbf)

}

Isr.close ()

Gzin.close ()

Return sb.toString ()

}

Public static void main (String [] args) throws Exception {

String paString = "ZHUaLE300APPhAVGY83szZX4gt4o6O7yv4MQf_uXTe_i8CUJMxJ9CBAcEH8SrTND-6AyeCKX dsS3aZ4bjyqqspdxKhpmv8KOQJ17kJStfhu6wlpNJILUzMeXCa_bwabJajNH HWUrnbILbHHadpd9TkqV1gIuDgDdmJ4GebNjFoaqKfE1EouwG4Dk-ew-x6My nypnmUhELyArFwFOCA230TDvxUUNv4CQEzbwWXKkbon6"

PaString=URLEncoder.encode (paString)

Try {

String result = sendJsonRequest ("http://localhost/uip/property/findProperty.do",paString);

} catch (Exception e) {

E.printStackTrace ()

}

}

}

The above is how to achieve APP HTTP interface message encryption and compression of all the content, more and how to achieve APP HTTP interface message encryption and compression-related content can search the previous articles or browse the following articles to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report