1
1
package com .baeldung .springbootsecurity .oauth2server ;
2
2
3
+ import org .junit .Before ;
3
4
import org .junit .Test ;
4
5
import org .junit .runner .RunWith ;
5
6
import org .springframework .boot .test .context .SpringBootTest ;
7
+ import org .springframework .boot .web .server .LocalServerPort ;
6
8
import org .springframework .security .oauth2 .client .OAuth2RestTemplate ;
7
9
import org .springframework .security .oauth2 .client .resource .OAuth2AccessDeniedException ;
8
10
import org .springframework .security .oauth2 .client .token .grant .client .ClientCredentialsResourceDetails ;
9
11
import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
10
12
import org .springframework .test .context .ActiveProfiles ;
11
13
import org .springframework .test .context .junit4 .SpringRunner ;
12
14
15
+ import java .net .URL ;
16
+ import java .util .regex .Pattern ;
17
+
13
18
import static java .util .Collections .singletonList ;
19
+ import static org .junit .Assert .assertEquals ;
14
20
import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertTrue ;
15
22
import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .RANDOM_PORT ;
16
23
17
24
@ RunWith (SpringRunner .class )
18
25
@ SpringBootTest (webEnvironment = RANDOM_PORT , classes = SpringBootAuthorizationServerApplication .class )
19
26
@ ActiveProfiles ("authz" )
20
27
public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
21
28
29
+ @ LocalServerPort
30
+ private int port ;
31
+
32
+ @ Before
33
+ public void setUp () throws Exception {
34
+ base = new URL ("http://localhost:" + port );
35
+ }
36
+
22
37
@ Test
23
38
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull () {
24
39
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails ("baeldung" , singletonList ("read" ));
@@ -27,7 +42,29 @@ public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIs
27
42
OAuth2AccessToken accessToken = restTemplate .getAccessToken ();
28
43
29
44
assertNotNull (accessToken );
45
+ }
46
+
47
+ @ Test
48
+ public void givenOAuth2Context_whenAccessingAuthentication_ThenRespondTokenDetails () {
49
+ ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails ("baeldung" , singletonList ("read" ));
50
+ OAuth2RestTemplate restTemplate = getOAuth2RestTemplate (resourceDetails );
51
+
52
+ String authentication = executeGetRequest (restTemplate , "/authentication" );
53
+
54
+ Pattern pattern = Pattern .compile ("\\ {\" remoteAddress\" :\" .*" +
55
+ "\" ,\" sessionId\" :null,\" tokenValue\" :\" .*" +
56
+ "\" ,\" tokenType\" :\" Bearer\" ,\" decodedDetails\" :null}" );
57
+ assertTrue ("authentication" , pattern .matcher (authentication ).matches ());
58
+ }
59
+
60
+ @ Test
61
+ public void givenOAuth2Context_whenAccessingPrincipal_ThenRespondBaeldung () {
62
+ ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails ("baeldung" , singletonList ("read" ));
63
+ OAuth2RestTemplate restTemplate = getOAuth2RestTemplate (resourceDetails );
64
+
65
+ String principal = executeGetRequest (restTemplate , "/principal" );
30
66
67
+ assertEquals ("baeldung" , principal );
31
68
}
32
69
33
70
@ Test (expected = OAuth2AccessDeniedException .class )
0 commit comments