Table of Contents
Reply content:
Home Backend Development PHP Tutorial In a framework where the front and back ends are separated, how to make unauthenticated jumps?

In a framework where the front and back ends are separated, how to make unauthenticated jumps?

Oct 22, 2016 am 12:14 AM
php php-fpm

In the framework of front-end and back-end separation, the back-end request is generally sent to the directory corresponding to the back-end through the nginx configuration, and then the front-end request goes to the directory corresponding to the front-end. Now I just happened to come across an authentication strip function. What needs to be implemented is that every request sent, whether it is a front-end request or a back-end request, first determines whether there is a login, and then jumps to the login homepage if there is no login.
1. Generally, authentication judgment is done by the back-end, and page jump is done by the front-end. How can these two steps be combined?

  1. When the front and back ends are not separated, a hook is usually set during the initialization of the framework to determine whether there is login, and jump if there is no login.

  2. I think the solution after separation should be that the backend still performs authentication judgment on each request and only returns without jumping. Then when the front-end framework module is initialized, it first requests the authentication interface. If the authentication fails, it jumps to the login page.

Experienced friends, please give me some advice on how to do this authentication in a separation scenario.

Reply content:

In the framework of front-end and back-end separation, the back-end requests are generally directed to the directory corresponding to the back-end through the configuration of nginx, and then the front-end requests are directed to the directory corresponding to the front-end. Now I just happened to come across an authentication strip function. What needs to be implemented is that every request sent, whether it is a front-end request or a back-end request, first determines whether there is a login, and then jumps to the login homepage if there is no login.
1. Generally, authentication judgment is done by the back-end, and page jump is done by the front-end. How can these two steps be combined?

  1. When the front and back ends are not separated, a hook is usually set during the initialization of the framework to determine whether there is login, and jump if there is no login.

  2. I think the solution after separation should be that the backend still performs authentication judgment on each request and only returns without jumping. Then when the front-end framework module is initialized, it first requests the authentication interface. If the authentication fails, it jumps to the login page.

Experienced friends, please give me some advice on how to do this authentication in a separation scenario.

Front-end separation usually uses json transmission. If it is Json, you can do this.
Returns when verification is successful: {code: "Success", data: "Data to be retrieved", info: null},
Returns when verification fails: {code: "Failure", data: null, info: "No access rights" "}
The front-end determines the code value after receiving it. As for whether to jump or not, that is the front-end's own business.

The way I implemented it is:

After the front-end logs in, save the token generated by the back-end (the back-end can be saved with JWT or redis, mysql, and the front-end can be saved in localstorage or cookie).

Every request from the front end requires authorization (login) interface to determine whether the token is valid (including whether it has expired). If it is invalid, it will return 403 and an error message. The front end uses a Service base class (for example, AdminService catch 403 exception, if caught, route to the login page), or write a global filter to catch 403.

Separating the front-end and back-end is to cache a login status on the front-end, and then bring this status code with each request, and the background determines whether it has expired. That’s pretty much it

It’s very simple. For example, for background projects, we all know that you can only enter the system after logging in successfully. So how do you do the authentication in the whole process?

  1. The backend filters all incoming requests to verify whether the user is logged in. If not, continue to ignore him. If not, let him jump directly to the login page

  2. For the front-end, every route jump must also be verified. For example: before opening a certain page, send an ajax to the back-end for verification. If the login page is not displayed, jump to the login page

  3. How to verify? When the user does not log in successfully, the browser does not write the cookie. After the user logs in successfully, the user's cookie is saved to the browser, and the user's session is saved to the server. The browser's cookie is the session key

Our company has recently completely separated the front and back ends. The specific method is very simple. For example, if there is a page that requires login verification, the front end will directly request the back end. The backend determines whether to log in. If logged in, the return status status is success, plus data and so on. If there is no login, it will directly return to a fixed status, that is, status is fail_401.

If there is no data that can be obtained on the page, we will directly provide a general interface. When entering the page, request the interface. If it passes, it will stay on the current page. If it does not pass, it will jump directly.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles