Skip to content

Commit 07893ce

Browse files
committed
1 parent 76322dc commit 07893ce

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

samples/demo-client/samples-demo-client.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ dependencies {
2929
implementation "org.webjars:bootstrap:5.2.3"
3030
implementation "org.webjars:popper.js:2.9.3"
3131
implementation "org.webjars:jquery:3.6.4"
32+
33+
testImplementation "org.springframework.boot:spring-boot-starter-test"
34+
testImplementation "org.springframework.boot:spring-boot-starter-oauth2-authorization-server"
35+
testImplementation("org.springframework.experimental.boot:spring-boot-testjars:0.0.1") {
36+
capabilities {
37+
requireCapability("org.springframework.experimental.boot:spring-boot-testjars-maven")
38+
}
39+
}
40+
testImplementation "org.springframework.boot:spring-boot-testcontainers"
41+
testImplementation "org.apache.commons:commons-exec:1.3"
3242
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2020-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package sample;
17+
18+
import testjars.authorizationserver.TestAuthorizationServerApplication;
19+
20+
import org.springframework.boot.test.context.TestConfiguration;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.experimental.boot.server.exec.CommonsExecWebServerFactoryBean;
23+
import org.springframework.experimental.boot.server.exec.MavenClasspathEntry;
24+
import org.springframework.experimental.boot.test.context.EnableDynamicProperty;
25+
import org.springframework.experimental.boot.test.context.OAuth2ClientProviderIssuerUri;
26+
27+
@EnableDynamicProperty
28+
@TestConfiguration(proxyBeanMethods = false)
29+
public class AuthorizationServerContainerConfig {
30+
31+
@Bean
32+
@OAuth2ClientProviderIssuerUri
33+
public CommonsExecWebServerFactoryBean authorizationServer() {
34+
// @formatter:off
35+
return CommonsExecWebServerFactoryBean.builder()
36+
.mainClass(TestAuthorizationServerApplication.class.getName())
37+
.classpath((classpath) -> classpath
38+
.entries(MavenClasspathEntry.springBootStarter("oauth2-authorization-server"))
39+
.recursive(TestAuthorizationServerApplication.class)
40+
);
41+
// @formatter:on
42+
}
43+
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package sample;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.http.HttpStatus;
23+
import org.springframework.http.ResponseEntity;
24+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
25+
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
26+
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
27+
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
28+
import org.springframework.util.LinkedMultiValueMap;
29+
import org.springframework.util.MultiValueMap;
30+
import org.springframework.web.client.RestClient;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Integration tests against a Spring Authorization Server using spring-boot-testjars.
36+
*
37+
* @see <a target="_blank" href="https://github.com/spring-projects-experimental/spring-boot-testjars">spring-boot-testjars</a>
38+
*/
39+
@SpringBootTest(classes = AuthorizationServerContainerConfig.class)
40+
public class AuthorizationServerIntegrationTests {
41+
42+
@Value("${spring.security.oauth2.client.provider.spring.issuer-uri}")
43+
private String issuerUri;
44+
45+
// @formatter:off
46+
private final RestClient restClient = RestClient.builder()
47+
.messageConverters(converters ->
48+
converters.add(0, new OAuth2AccessTokenResponseHttpMessageConverter()))
49+
.build();
50+
// @formatter:on
51+
52+
@Test
53+
public void requestWhenTokenRequestClientCredentialsGrantThenTokenResponse() {
54+
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
55+
params.add(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue());
56+
params.add(OAuth2ParameterNames.SCOPE, "scope-1");
57+
58+
// @formatter:off
59+
ResponseEntity<OAuth2AccessTokenResponse> tokenResponse = this.restClient.post()
60+
.uri(this.issuerUri + "/oauth2/token")
61+
.body(params)
62+
.headers((headers) -> headers.setBasicAuth("client-1", "secret"))
63+
.retrieve()
64+
.toEntity(OAuth2AccessTokenResponse.class);
65+
// @formatter:on
66+
67+
assertThat(tokenResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
68+
assertThat(tokenResponse.getBody().getAccessToken()).isNotNull();
69+
}
70+
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package testjars.authorizationserver;
17+
18+
import java.util.UUID;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
23+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
24+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
25+
import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
26+
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
27+
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
28+
29+
@EnableWebSecurity
30+
@Configuration(proxyBeanMethods = false)
31+
public class AuthorizationServerConfig {
32+
33+
@Bean
34+
public RegisteredClientRepository registeredClientRepository() {
35+
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
36+
.clientId("client-1")
37+
.clientSecret("{noop}secret")
38+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
39+
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
40+
.scope("scope-1")
41+
.build();
42+
43+
return new InMemoryRegisteredClientRepository(registeredClient);
44+
}
45+
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package testjars.authorizationserver;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
@SpringBootApplication
22+
public class TestAuthorizationServerApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(TestAuthorizationServerApplication.class, args);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)