Simple demonstration of third-party login with Spring Security OAuth2 Provider

For testing, 3 different Spring Boot applications need to be created as follows:
  • spring-oauth2-client client (port: 8080)
  • spring-oauth2-server Auth server (port: 8081)
  • spring-oauth2-resource Rest server (port: 8082)

Version
ScribeJava 4.1.2 + Spring Security OAuth2 2.0.12

ScribeJava itself has provided many connection APIs of OAuth development platform, here you need to customize the API for testing the Auth server.
The Auth server and Rest server connect to the same backend PostgreSQL database.

The specific implementation can download the source code to view. Click to download

related articles: Spring
Security OAuth2 Provider Minimum Implementation Spring Security OAuth2
Provider Database Storage Visit the client home page http://localhost:8080/ (2) Click the signin link http://localhost:8080/signin (3) During signin processing, jump to the Auth server-side authentication via ScribeJava http://localhost:8081 /oauth/authorize?...










final OAuth20Service service = new ServiceBuilder(CLIENT_ID)
		.apiSecret(CLIENT_SECRET)
		.scope(SCOPE)
		.state(STATE)
		.callback(CALLBACK_URL)
		.responseType(RESPONSE_TYPE)
		.build(MyApi.instance());

final String authorizationUrl = service.getAuthorizationUrl();

response.sendRedirect(authorizationUrl);


(4) The Auth server enters the username and password

(5) The Auth server authorizes

(6) The Auth server calls back the client and returns the code value http://localhost:8080/callback

(7) In the callback processing, use the code value Send a request to the Auth server via ScribeJava to get AccessToken http://localhost:8081/oauth/token?…
final OAuth20Service service = new ServiceBuilder(CLIENT_ID)
		.apiSecret(CLIENT_SECRET)
		.scope(SCOPE)
		.state(STATE)
		.callback(CALLBACK_URL)
		.build(MyApi.instance());

OAuth2AccessToken accessToken = service.getAccessToken(code);


(8) In callback processing, use AccessToken to send a request to the Rest server via ScribeJava to obtain user information http://localhost:8082/api/profile
final OAuth20Service service = new ServiceBuilder(CLIENT_ID)
		.apiSecret(CLIENT_SECRET)
		.scope(SCOPE)
		.state(STATE)
		.callback(CALLBACK_URL)
		.build(MyApi.instance());

final OAuthRequest oauthRequest = new OAuthRequest(Verb.GET, PROFILE_URL);
service.signRequest(accessToken, oauthRequest);
final Response resourceResponse = service.execute(oauthRequest);


(9) During callback processing, the user information is obtained and displayed to the client
JSONObject obj = new JSONObject(resourceResponse.getBody());
model.addAttribute("id", obj.getString("id"));
model.addAttribute("name", obj.getString("name"));
model.addAttribute("email", obj.getString("email"));

Guess you like

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