Skip to content

Commit 220a0bf

Browse files
committed
add user-info endpoint live test
1 parent c23f5e1 commit 220a0bf

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthServerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void configure(final ClientDetailsServiceConfigurer clients) throws Excep
3030
.authorizedGrantTypes("authorization_code")
3131
.scopes("user_info")
3232
.autoApprove(true)
33-
.redirectUris("http://localhost:8082/ui/login","http://localhost:8083/ui2/login","http://localhost:8082/login")
33+
.redirectUris("http://localhost:8082/ui/login","http://localhost:8083/ui2/login","http://localhost:8082/login","http://www.example.com/")
3434
// .accessTokenValiditySeconds(3600)
3535
; // 1 hour
3636
}

spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ protected void configure(HttpSecurity http) throws Exception { // @formatter:off
2222
.authenticated()
2323
.and()
2424
.formLogin()
25-
.permitAll();
25+
.permitAll()
26+
.and().csrf().disable();
2627
} // @formatter:on
2728

2829
@Override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.baeldung;
2+
import static org.junit.Assert.assertEquals;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.junit.Test;
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.HttpStatus;
10+
11+
import io.restassured.RestAssured;
12+
import io.restassured.response.Response;
13+
14+
public class UserInfoEndpointLiveTest {
15+
16+
@Test
17+
public void givenAccessToken_whenAccessUserInfoEndpoint_thenSuccess() {
18+
String accessToken = obtainAccessTokenUsingAuthorizationCodeFlow("john","123");
19+
Response response = RestAssured.given().header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken).get("http://localhost:8081/auth/user/me");
20+
21+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
22+
assertEquals("john", response.jsonPath().get("name"));
23+
}
24+
25+
private String obtainAccessTokenUsingAuthorizationCodeFlow(String username, String password) {
26+
final String authServerUri = "http://localhost:8081/auth";
27+
final String redirectUrl = "http://www.example.com/";
28+
final String authorizeUrl = authServerUri + "/oauth/authorize?response_type=code&client_id=SampleClientId&redirect_uri=" + redirectUrl;
29+
final String tokenUrl = authServerUri + "/oauth/token";
30+
31+
// user login
32+
Response response = RestAssured.given().formParams("username", username, "password", password).post(authServerUri + "/login");
33+
final String cookieValue = response.getCookie("JSESSIONID");
34+
35+
// get authorization code
36+
RestAssured.given().cookie("JSESSIONID", cookieValue).get(authorizeUrl);
37+
response = RestAssured.given().cookie("JSESSIONID", cookieValue).post(authorizeUrl);
38+
assertEquals(HttpStatus.FOUND.value(), response.getStatusCode());
39+
final String location = response.getHeader(HttpHeaders.LOCATION);
40+
final String code = location.substring(location.indexOf("code=") + 5);
41+
42+
// get access token
43+
Map<String, String> params = new HashMap<String, String>();
44+
params.put("grant_type", "authorization_code");
45+
params.put("code", code);
46+
params.put("client_id", "SampleClientId");
47+
params.put("redirect_uri", redirectUrl);
48+
response = RestAssured.given().auth().basic("SampleClientId", "secret").formParams(params).post(tokenUrl);
49+
return response.jsonPath().getString("access_token");
50+
}
51+
}

0 commit comments

Comments
 (0)