spring social understanding

      Nowadays, with the rapid development of the Internet, people surf the Internet every day to obtain various information. In order to facilitate user login, major websites provide various social logins, such as QQ, WeChat and Weibo logins. Most of these mainstream social logins are implemented based on the oauth protocol. Spring social encapsulates the oauth protocol, which is convenient for users to quickly log in with a third-party system.

       Take our QQ login as an example, we click the QQ login icon on the page, the website will navigate us to the QQ login server, we complete the login on the QQ login server, and then go back to our own server. After completing the user's QQ login, we can also obtain the user information of QQ.

      In the above QQ login process, after the user successfully logs in on the QQ server, he returns to our own system. At this time, the QQ server returns us an authorization code ( code ), and then the application operates in the background and sends a request to The QQ server exchanges our access token ( access_token ) according to this authorization code (code), and then obtains the user's information according to the access token (access_token).

     Then with the above process, we need to obtain user information, but the user information returned by different server providers may be different. For example, QQ returns some user information (username, image), and WeChat returns some user information (nickname, gender), then we need to adapt these user attributes to become identifiable by spring social, then we need ApiAdapter to adapt.

     For the application of QQ and WeChat, we have to deal with them and obtain user information. For us, QQ and WeChat are a service provider ( ServiceProvider ), and we want to obtain information from these service providers. To finish their protocol (oauth), and the authorization part of the oauth protocol is roughly the same, in spring social this part has ( Oauth2Operations ) to complete.

     When our client application establishes a connection with the server (after completing the oauth protocol ), a connection ( Connection ) is formed, allowing the application to call the service provider's api to obtain or update the user's information.

     Connection is created by ConnectionFactory , and different protocols (oauth1/oauth2...) correspond to different ConnectionFactory implementations.

     After Zhang San uses QQ to log in to the system, how do we know that it is Zhang San corresponding to the business system? At this time, there should be a UserConnection table to represent the corresponding relationship. UserConnection needs to be persisted in the database or saved in memory, then at this time It is done by the implementation of UserConnectionRepository

 

     From the above simple understanding, we briefly summarize. We need to go through the authorization process for authorization with the service provider ( ServiceProvider<A> ), which is completed by Oauth2Operations at this time . After the authorization process is completed, we can obtain the access token ( access_token ), and then call the api to obtain user information. However, different service providers may return different user information. In this case, the user information needs to be adapted to meet the format required by spring social. At this time, ApiAdapter is required for adaptation. Different protocols (oauth1/oauth2...) construct connections ( Connection ) differently, that is, ConnectionFactory is needed to encapsulate different protocols to form a connection (Connection).

 

ConnectionFactory is composed of ServiceProvider and ApiAdapter (ConnectionFactory generates Connection, but the generation of Connection needs to go through the authorization process, but the structure of Connection is fixed, and the user information returned by each service provider is the same, so ApiAdapter is required for adaptation.)

 

ServiceProvider consists of Oauth2Operations and Api

Api is an interface written by the user himself. The user deals with the service provider and obtains the information of the third-party user on the service provider.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340377&siteId=291194637