The project is separated from the front and back. The back-end only provides front-end interfaces, and each interface has a token. Now there are some pages that require login to enter. How to intercept the front-end
. For example: jump from A page
through the A tag to B page
(B page needs to be logged in to enter). My current approach is to enter page B, send an ajax request
, and then jump to the login page after the background returns the status. The problem now is that it is unreasonable for me to make a judgment only after I have entered page B.
You can consider sending an ajax request first when clicking on the a tag and then decide where to jump?
You can encapsulate an ajax request by yourself, judge the login status separately, jump to login if you are not logged in, and return data directly after logging in
Write a public js, write the page loading function in it, reference the page that you need login authorization to access, and then write logical judgment in it
The front-end does not intercept, the server does interception. For front-end interception, it can only be done on page B. Have you considered spelling the URL to enter? The B page access interface tries to get userinfo. If you get it, you are logged in. If you don't get it, you are not logged in.
Tokens generally have an expiration time. When saving tokens in localstorage, bring an expiration time. When the page jumps, first determine whether the token
has expired. If it expires, it will jump to the login page. If not, continue.
If it is a single-page application, this is easy to solve. If it is not a single-page application, just write it in a public js.
The user will definitely jump to the second page due to some interactive action. Login verification will be performed before jumping. If they are not logged in, they will be asked to log in (or register). The login (registration) must be encapsulated. Encapsulated Reserve a successful callback after login and a callback after successful registration, and then jump to page B in the callback function
There are two ways to solve this problem:
1. The server performs verification since it is a page that requires logging in to enter, which will inevitably involve user permissions. There is a token in the ajax request sent by page B. The backend performs token expiration verification to determine whether there is permission. When errors are reported, return to the front end and write a public method to verify the error message without permission (this method should be more secure on the response interface)
2. The front end writes a watchdog js on each page to determine the expiration of stored user information. Time is jumping.
I think the poster’s request is to determine whether to jump when clicking. You can change the a tag into a click function and judge in the function to jump to the page using window.location.href = url
What is the lz front-end framework used for?
I know that there is a hook function in vue_router that can be implemented. In router.beforeEach, it is judged whether to log in. If not, log in. If logged in, just next()