1.APP Back-end development series: Attention problems in the design of landing system

Source: Internet
Author: User

GitHub Blog-Starting address

Want to write this series for a long time, because before doing this thing spent a lot of energy, it is necessary to share to share with you. I've also written a series of articles about app backend development because of the lack of skill, many problems are not clearly described or the solution is too complex, not rigorous and so on.

This time I looked up a lot of information and asked a lot of relevant people. Be prepared to add some more to the problems of your actual work. Let's start with the landing design!

The more you think, the more confused

Before doing this part of the time, always thinking about the complex technology, say to how much cock bombing days ah. Generally, the process of landing is:

At that time, the security over-obsessed, did not take the road. The first time the submission of fear of information is hijacked, so the client in the upload, AES encryption, the service side decrypted results. The information returned by the server is also encrypted by AES and then decrypted by the client.

Then here comes another problem: the encryption information on the client, then one but the client is anti-compilation, Hacker to get the secret key, then for the service side encryption is meaningless. And in order not to save such sensitive information on the client, like the secret key is issued by the server. This way the server can make changes to the key at any time.

Here again brings a new problem, sense of foot everything back to the beginning: The secret key to go HTTP, then still may be hijacked. Is it time to encrypt or how? If encrypted, the client then puts a secret key past. The secret key is still likely to be decompile. Can't get this key from the server anymore? To ensure the security of the last secret key, produce the next secret key ...

At that time was caught in such a cycle of death, can not extricate themselves. Now think about it, it's too young too simple!.

Simple and effective

First here or first: If your product is just getting started, don't be too obsessed with performance, security.

First of all, performance: When your product is launched, the number of cold-start users will generally not exceed 1000 (this is already a very cold start number of people). Then you will not be more than 100 concurrent. This level of access, believe that the machine hardware can help you solve. If your condition goes well beyond that, your strength is absolutely enough to cope with what is about to happen.
Talk about security: The security thing, from the very beginning to consider, but not too tangled (I can later talk about my text verification code in this part of the experience). Premature intervention can lead to slower system development, too early to do something you don't need (hacker you also need cost, no one wants to come to you when you are worthless). So early should pay attention to development costs, seize the time, early on-line.

In addition, safety and performance are sometimes fish and bear paws.

The road of evolution

This section will have some code and diagrams to illustrate. Gradually evolve in terms of security. Said earlier development as soon as possible, but here also pay attention to a problem, is that the subsequent upgrade can make up for the previous error, to the future upgrade left room. Because otherwise your system will always leave a hidden danger.

The realization of the function is good

This is the beginning of the phase, with a focus on functional implementation. After the user submits the username + PWD Service-side verification pass, a token token is returned.

Here are a few parts to be aware of in order to prepare for future upgrades. Some of the initial designs that I often encounter are:

  1. After the validation passes, the user uid+username+salt such MD5 and returns to the client as token.
  2. Add timestamp to token, expire after client resubmit username + PWD authentication and then send a token to client
  3. The server generates a token and sends it to the client, and the client requests the server after encrypting it according to the agreed rules.

First of all, the first problem: the generated token is permanent, then someone else gets a token can be unlimited requests. Until you close the interface. Added cost to subsequent security designs.

The second kind of problem is a bit old fire, although it seems that token only in a certain time range effective, but in fact more unsafe. First, the client needs to save the user's user name and password, if the user's phone is usually not focused on security, it is easy to be stolen.

The third design, this is one of the things I did, is the most rubbish design of these three kinds of schemes. The lesson is that you must never give anything encrypted to the client. This kind of flexibility is greatly compromised. Example: or upgrade interface, now the token generation is just the service side of the thing, the server can change the rules at any time, now because the client is also involved in, this matter is troublesome, you change, the client also to follow change. There is no flexibility to speak of. Remember: The client receives it and forwards it back to the server. Do not encrypt the client again!!!

After the experience of these pits, referring to oauth2.0, I now use the following options:

After the user submits username + pwd, the server returns the following information:

{    "access_token":"2YotnFZFEjr1zCsicMWpAA",    "expires_in":3600,    "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA"}

Access_token is used to access the interface,expires_in is his expiration time, reached the expiration time, need to use Refresh_token to request server-side Refresh Access_token.

Here are a few key points:Refresh_token can only be used once, once used, will be discarded. In addition, this Access_token is valid only within the expires_in validity period.

Note: This expires_in only returns the number of seconds. Do not return a timestamp. Because each platform calculates the time stamp of S, it is more convenient to handle this as it is inconsistent.

Access Frequency Control

Above we simply implemented the function, now the flow of the app comes up, some features are also very complex, if an interface is too large, will cause the server to crash, you need to each interface each access to set the frequency (also can be unified to set the frequency of each interface access).

In general my practice is to add a middleware. Each interface accesses the frequency to make a corresponding configuration file. Like what:
* A interface 5s can be accessed 1 times
* The B interface can be accessed 1 times in 10s (it can be very time consuming, if too many requests can cause the server to crash)

Then connect access_token with these. Redis needs to be used here. When user A comes in and accesses the A interface then set this token 5s to not be accessed again.

    if ($redis->get($key)) {        // 无法访问,还未到时间        return ;    }    // 设置频率控制key    $redis->setex($key$expires$value);    // 访问接口

Here are a few questions to consider:

Set the access time to reasonable. Example: When a client typically initiates a request for multiple interfaces, the server may reject some of the access after the request arrives (because within the frequency control)

In general, there is no need for frequency control of all interfaces, only for the important content and performance of the interface required for frequency control.

Account Security Considerations

Now further, need to consider user account security issues. For example: QQ, sometimes remind us that your account has landed in Hong Kong. If it's not what you do, change your password or something.

To do this, you need to record the IP address of each token each time you log in and start. If the IP address is not in the same range as the last IP (this rule is determined by itself, because some carrier IP often changes, such as: Great Wall). Just remind the user whether he or she is doing it, and if not, quickly change the password.

Many apps are now available at the beginning of the development and can be logged in simultaneously with multiple devices. This brings a lot of security problems. If you want to make a single device landing, you need each token to correspond to a devicetoken.

This part is not going to be discussed in depth.

Anti-DNS hijacking

The security work is done well, if someone can get a lot of legitimate user token, to ask for your excuses, you can not identify, because from the behavior, it is all legitimate users.

In the past to prevent others from obtaining legitimate information, I have made a very dog blood client encryption method. Causes a lot of problems during the post-upgrade. This thing is actually very simple, use HTTPS to make requests (can use personal critical interface)

Token

When I started to do the app service, I always thought about the design of token. How can you generate a good token? Now I don't know what I was thinking about.

Generation of Tokens

The first thing to figure out is that this token is a token that marks a user's identity. So first he wants the only. Next he uploads from the client, the server can verify that the token is generated by the server.

So token generation as long as you meet the above purposes, you are free to do so. Of course, don't expose the sensitive information.

A common way to generate:

    1. The UID of the user, such as: 8888
    2. The user's password, such as: 123123
    3. The user's corresponding salt, such as: ABCD
    4. Expiration timestamp, such as: 1468293948

Stitching up the above sections: 888:123123:abcd:1468293948

token = MD5 (' 888:123123:abcd:1468293948 ');

Token verification

There are two ways to validate tokens. One is: After the server is generated, the token is saved (Redis or MySQL). After the client wears it, check to see if it has the token, and if it has the corresponding information, such as UID, verify that it matches.

Another method is to generate the corresponding token based on the UID of the upload, and then compare the token result to be consistent (to guarantee the algorithm if the given value is certain, the result must be unique. Common MD5).

For individuals, the second option is preferred. The first scenario is more efficient (you can use Redis to store the token), but if Redis is an avalanche, it will invalidate all user logins and not log in for a certain amount of time. Initially the simpler, the more reliable and better.

Summarize

This part does not have much code, mainly is the idea. There are also issues related to the landing of H5 not said. The next article will be in the app after landing, if the problem of H5 landing to elaborate.

1.APP Back-end development series: Attention problems in the design of landing system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.