Vue single sign-on method

 

  • Vue single sign-on method

    When we use vue to develop a project, there is usually only one user account. If we want to implement single sign-on for multiple accounts, we can use the two technologies of Session and LocalStorage. When these two technologies implement single sign-on, they all need a user name and a password, and the user's login information can be obtained through session and localStorage. But these two technologies have their own disadvantages: 1. session: every time a user registers, a session is generated, but each registration consumes a certain amount of memory. 2. LocalStorage: Each time a user registers, there will be a LocalStorage, but when the user logs out of the account, the LocalStorage of the account will be cleared. So, how do we implement single sign-on in the vue project? Let's take Vue. js as an example to introduce. 1. First we need to create a userid and save the userid and password together in the template variable. 2. Then create a customer username and customer.parse nt () function in vue. 3. Save the userid and customer username together in the template variable, and use session and localStorage to obtain the user's login information.

    • 1. Create a userid and save the userid and password together in the template variable

      Here we are using the vue-based mode. The advantage is that it can provide a separate userid for each account. We only need to give each account a userid, and there is no need to give each account a separate password. 1. Create userid 2. In the vue-based mode, we only need to assign a userid to each user, and do not need to assign a password to each user individually. 3. Save the user name and password 6. Then we can get the user's login information through session and localStorage.

    • 2. Create customer username and customer.parse nt () function

      (1) Add the customer.parse nt function in customer, and when the user logs in, the user information will be automatically obtained from the template. (2) In the customer.parse nt function, add the customer.pos_str () method, so that when the user logs in from a different device, the user name and password will be obtained. (4) In the customer.parse nt function, add the session method, and use the session to obtain the user's login information.

    • 3. Obtain the user's login information through session and localStorage in the vue project

      session is a very simple method, it can get the user's login information by creating a session object, and every time the user logs in, a new session object will be created. So how to get the user's login information? In fact, it is also very simple, we only need to create a template variable in the vue project, and save the userid and customer username in the variable.

    • 4. Select the user on the page

      Click customer on the page, enter the user's name and password into customer, and then click the save button, so that the user's login information can be successfully obtained. In this way, single sign-on can be implemented on multiple platforms. The above is the vue single sign-on method I introduced, I hope it can be helpful to you! If you have any questions or suggestions, you can contact me at any time.

    • 5. Click the login button to obtain the user's login information

      When using vue projects, we can also use some other technologies, such as the Document.wrapper () function to achieve single sign-on, and the Document.cleaner () function to achieve single sign-on, and Document.wrapper () function to implement single sign-on.

    • 6. Finally, the login information is sent to the web server

      We can install a proxy on the web server, use the proxy to obtain data from the web server, and then send the data to the session through the proxy. Session is a common communication method. When using session, we need to use some session.xml files to specify what information we want to obtain. 1. We can specify session parameters by adding a configuration file on the web server, such as session.xml file: 2. Add a configuration file on the web server, through which we can specify what information we want to obtain . For example, we can specify that the user name is userid, and the user password is the parameter of the customer.parse nt () function. 3. After setting these parameters, add a proxy on the web server, and then send the information we want to obtain to the proxy. 4. The purpose of adding a proxy on the web server is to obtain the data of the web server. When we need to send the data of the web server to the session, we can obtain the data through the proxy on the web server.

    • 7. Exit the login page to obtain the user's login information

      The disadvantage of session and localStorage is that the user's login information will be cleared after the user logs out. If you want to clear the user's login information when the user logs out, you can use the reload () function. In vue.js, the reload () function is used to clear the user's login information when the user logs out. First, you need to set the parameters of the reload () function through two parameters: 1. If you want to clear the login information when the user logs out , you need to set the parameter of the reload() function to null:

  • There are many ways to implement Vue single sign-on, the following is one of the commonly used ways:

  • 1. After successful login, store user information in localStorage:
    ```javascript
    // After successful login, store user information in localStorage
    localStorage.setItem('userInfo', JSON.stringify(userInfo));
    `` `
    2. On the page that requires single sign-on, judge whether the user has logged in by judging whether there is user information in localStorage: ```
    javascript
    // Judging whether the user has logged in
    if (!localStorage.getItem('userInfo')) { // Not logged in, jump to the login page window.location.href = '/login'; } ``` 3. On the page that needs to perform single-point exit, clear the user information in localStorage: ```javascript // Log out and clear the user information in localStorage localStorage.removeItem('userInfo'); ``` 4. On the page that needs to obtain user information, obtain user information from localStorage: ```javascript // Obtain user information const userInfo = JSON. parse(localStorage. getItem('userInfo'));













    ```
    The above is a simple implementation of Vue single sign-on, and the specific implementation needs to be adjusted according to specific business needs.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130993051