Skip to content

Commit 7f06cef

Browse files
authored
Merge pull request #8 from hammadirshad/spring-3.5
Spring 3.5
2 parents f264f82 + 9128145 commit 7f06cef

30 files changed

+281
-1162
lines changed

authorization-code/pom.xml

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,93 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
<parent>
7-
<groupId>com.example</groupId>
8-
<artifactId>helseid-client-parent</artifactId>
9-
<version>${revision}</version>
10-
<relativePath>../pom.xml</relativePath>
11-
</parent>
12-
<artifactId>authorization-code</artifactId>
13-
<name>HelseID authorization code</name>
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.example</groupId>
8+
<artifactId>helseid-client-parent</artifactId>
9+
<version>${revision}</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
<artifactId>authorization-code</artifactId>
13+
<name>HelseID authorization code</name>
1414

15-
<dependencies>
16-
<dependency>
17-
<groupId>com.example</groupId>
18-
<artifactId>common</artifactId>
19-
</dependency>
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.example</groupId>
18+
<artifactId>common</artifactId>
19+
</dependency>
2020

21-
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-web</artifactId>
24-
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
2525

26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
29-
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
29+
</dependency>
3030

31-
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-starter-security</artifactId>
34-
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-security</artifactId>
34+
</dependency>
3535

36-
<dependency>
37-
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
39-
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
39+
</dependency>
4040

41-
<dependency>
42-
<groupId>org.springframework.security</groupId>
43-
<artifactId>spring-security-oauth2-client</artifactId>
44-
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.security</groupId>
43+
<artifactId>spring-security-oauth2-client</artifactId>
44+
</dependency>
4545

46-
<dependency>
47-
<groupId>org.springframework.security</groupId>
48-
<artifactId>spring-security-oauth2-jose</artifactId>
49-
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.security</groupId>
48+
<artifactId>spring-security-oauth2-jose</artifactId>
49+
</dependency>
5050

51-
<dependency>
52-
<groupId>org.json</groupId>
53-
<artifactId>json</artifactId>
54-
</dependency>
51+
<dependency>
52+
<groupId>org.json</groupId>
53+
<artifactId>json</artifactId>
54+
<version>20211205</version>
55+
</dependency>
5556

56-
<dependency>
57-
<groupId>org.springframework.boot</groupId>
58-
<artifactId>spring-boot-configuration-processor</artifactId>
59-
<optional>true</optional>
60-
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-configuration-processor</artifactId>
60+
<optional>true</optional>
61+
</dependency>
6162

62-
<dependency>
63-
<groupId>org.bouncycastle</groupId>
64-
<artifactId>bcpkix-jdk18on</artifactId>
65-
</dependency>
63+
<dependency>
64+
<groupId>com.sun.activation</groupId>
65+
<artifactId>jakarta.activation</artifactId>
66+
</dependency>
6667

67-
<dependency>
68-
<groupId>org.apache.httpcomponents.client5</groupId>
69-
<artifactId>httpclient5</artifactId>
70-
</dependency>
68+
<dependency>
69+
<groupId>com.sun.xml.bind</groupId>
70+
<artifactId>jaxb-impl</artifactId>
71+
</dependency>
7172

72-
<dependency>
73-
<groupId>org.projectlombok</groupId>
74-
<artifactId>lombok</artifactId>
75-
</dependency>
76-
</dependencies>
73+
<dependency>
74+
<groupId>com.sun.xml.bind</groupId>
75+
<artifactId>jaxb-core</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.bouncycastle</groupId>
80+
<artifactId>bcpkix-jdk18on</artifactId>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.apache.httpcomponents.client5</groupId>
85+
<artifactId>httpclient5</artifactId>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.projectlombok</groupId>
90+
<artifactId>lombok</artifactId>
91+
</dependency>
92+
</dependencies>
7793
</project>

client-credentials/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@
6969
<artifactId>lombok</artifactId>
7070
</dependency>
7171
</dependencies>
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<configuration>
78+
<source>16</source>
79+
<target>16</target>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
7284
</project>

client-credentials/src/main/java/com/example/ClientCredentialsExample.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ public class ClientCredentialsExample implements ApplicationRunner {
2929
public void run(ApplicationArguments args) {
3030
String requestUrl = "http://localhost:9090/api/client-name";
3131

32-
OAuth2AccessToken accessToken = helseIDClientCredentialTokenService.getAccessToken();
33-
request(accessToken, requestUrl);
32+
try {
33+
OAuth2AccessToken accessToken = helseIDClientCredentialTokenService.getAccessToken();
34+
request(accessToken, requestUrl);
35+
} catch (Exception e) {
36+
log.error(e.getMessage(), e);
37+
}
38+
39+
log.info("----------------");
3440

35-
DPoPToken dPoPToken =
36-
helseIDDPoPClientCredentialTokenService.getAccessToken(requestUrl, HttpMethod.GET.name());
37-
request(dPoPToken, requestUrl);
41+
try {
42+
DPoPToken dPoPToken =
43+
helseIDDPoPClientCredentialTokenService.getAccessToken(requestUrl, HttpMethod.GET.name());
44+
request(dPoPToken, requestUrl);
45+
} catch (Exception e) {
46+
log.error(e.getMessage(), e);
47+
}
3848
}
3949

4050
private void request(OAuth2AccessToken accessToken, String requestUrl) {
Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,127 @@
11
package com.example.config;
22

3+
import com.example.security.dpop.DPoPHttpHeadersConverter;
34
import com.example.security.dpop.DPoPProofBuilder;
4-
import com.example.security.dpop.client.DPoPAccessTokenResponseClient;
5-
import com.example.security.dpop.client.DefaultDPoPAccessTokenResponseClient;
6-
import com.example.security.dpop.request.DPoPClientCredentialsGrantRequest;
7-
import com.example.security.dpop.request.DPoPOClientCredentialsGrantRequestEntityConverter;
85
import com.example.service.AuthorizationDetailsJwtClientParametersConverter;
96
import com.example.service.HelseIDClientCredentialTokenService;
107
import com.example.service.HelseIDDPoPClientCredentialTokenService;
118
import java.time.Duration;
129
import lombok.extern.slf4j.Slf4j;
13-
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
10+
import org.springframework.beans.factory.annotation.Qualifier;
11+
import org.springframework.boot.autoconfigure.security.oauth2.client.ConditionalOnOAuth2ClientRegistrationProperties;
1412
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1513
import org.springframework.context.annotation.Bean;
16-
import org.springframework.context.annotation.Conditional;
1714
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.context.annotation.Primary;
16+
import org.springframework.core.convert.converter.Converter;
17+
import org.springframework.http.HttpHeaders;
1818
import org.springframework.security.oauth2.client.ClientCredentialsOAuth2AuthorizedClientProvider;
1919
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
20-
import org.springframework.security.oauth2.client.endpoint.AbstractOAuth2AuthorizationGrantRequest;
21-
import org.springframework.security.oauth2.client.endpoint.DefaultClientCredentialsTokenResponseClient;
2220
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
2321
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest;
24-
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequestEntityConverter;
22+
import org.springframework.security.oauth2.client.endpoint.RestClientClientCredentialsTokenResponseClient;
2523
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2624
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
25+
import org.springframework.util.MultiValueMap;
2726

2827
@Slf4j
2928
@Configuration
3029
@EnableConfigurationProperties({
31-
OAuth2ClientHelseIDProperties.class,
30+
OAuth2ClientHelseIDProperties.class,
3231
})
33-
@Conditional(ClientsConfiguredCondition.class)
32+
@ConditionalOnOAuth2ClientRegistrationProperties
3433
public class HelseIDClientCredentialConfiguration {
3534

3635
private static final String HELSEID_CREDENTIALS = "helseid-credentials";
3736

3837
@Bean
39-
public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
40-
authorizationCredentialsGrantResponseClient(
38+
public DPoPProofBuilder dPoPProofBuilder(
4139
OAuth2ClientDetailProperties oauth2ClientKeypairProperties) {
42-
DefaultClientCredentialsTokenResponseClient tokenResponseClient =
43-
new DefaultClientCredentialsTokenResponseClient();
40+
return new DPoPProofBuilder(oauth2ClientKeypairProperties.getRegistration());
41+
}
4442

45-
OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter =
46-
new OAuth2ClientCredentialsGrantRequestEntityConverter();
43+
@Bean
44+
@Primary
45+
public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
46+
authorizationCredentialsGrantResponseClient(
47+
OAuth2ClientDetailProperties oauth2ClientKeypairProperties) {
48+
49+
RestClientClientCredentialsTokenResponseClient tokenResponseClient =
50+
new RestClientClientCredentialsTokenResponseClient();
4751

48-
requestEntityConverter.addParametersConverter(
52+
tokenResponseClient.addParametersConverter(
4953
new AuthorizationDetailsJwtClientParametersConverter<>(
5054
oauth2ClientKeypairProperties.getRegistration()));
5155

52-
tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
56+
return tokenResponseClient;
57+
}
58+
59+
@Bean
60+
public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
61+
authorizationCredentialsGrantResponseDpopClient(
62+
DPoPProofBuilder dPoPProofBuilder,
63+
OAuth2ClientDetailProperties oauth2ClientKeypairProperties) {
64+
65+
Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>>
66+
jwtClientParametersConverter =
67+
new AuthorizationDetailsJwtClientParametersConverter<>(
68+
oauth2ClientKeypairProperties.getRegistration());
69+
70+
Converter<OAuth2ClientCredentialsGrantRequest, HttpHeaders> dpopClientParametersConverter =
71+
new DPoPHttpHeadersConverter<>(jwtClientParametersConverter, dPoPProofBuilder);
72+
73+
RestClientClientCredentialsTokenResponseClient tokenResponseClient =
74+
new RestClientClientCredentialsTokenResponseClient();
75+
tokenResponseClient.addParametersConverter(jwtClientParametersConverter);
76+
77+
tokenResponseClient.addHeadersConverter(dpopClientParametersConverter);
78+
5379
return tokenResponseClient;
5480
}
5581

5682
@Bean
5783
public HelseIDClientCredentialTokenService helseIDClientCredentialTokenService(
5884
ClientRegistrationRepository clientRegistrationRepository,
5985
OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
60-
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
61-
credentialsGrantResponseClient) {
86+
@Qualifier("authorizationCredentialsGrantResponseClient")
87+
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
88+
authorizationCredentialsGrantResponseClient) {
6289

6390
ClientRegistration clientRegistration =
6491
clientRegistrationRepository.findByRegistrationId(HELSEID_CREDENTIALS);
6592

66-
ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider = new ClientCredentialsOAuth2AuthorizedClientProvider();
93+
ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider =
94+
new ClientCredentialsOAuth2AuthorizedClientProvider();
6795
clientCredentialsAuthorizedClientProvider.setAccessTokenResponseClient(
68-
credentialsGrantResponseClient);
96+
authorizationCredentialsGrantResponseClient);
6997

7098
return new HelseIDClientCredentialTokenService(
7199
clientRegistration,
72100
oAuth2AuthorizedClientService,
73101
clientCredentialsAuthorizedClientProvider);
74102
}
75103

76-
@Bean
77-
public DPoPProofBuilder dPoPProofBuilder(
78-
OAuth2ClientDetailProperties oauth2ClientKeypairProperties) {
79-
return new DPoPProofBuilder(oauth2ClientKeypairProperties.getRegistration());
80-
}
81-
82-
@Bean
83-
public DPoPAccessTokenResponseClient<DPoPClientCredentialsGrantRequest>
84-
authorizationCredentialsGrantResponseDpopClient(DPoPProofBuilder dPoPProofBuilder,
85-
OAuth2ClientDetailProperties oauth2ClientKeypairProperties) {
86-
87-
AuthorizationDetailsJwtClientParametersConverter<AbstractOAuth2AuthorizationGrantRequest>
88-
parametersConverter =
89-
new AuthorizationDetailsJwtClientParametersConverter<>(
90-
oauth2ClientKeypairProperties.getRegistration());
91-
92-
DPoPOClientCredentialsGrantRequestEntityConverter requestEntityConverter =
93-
new DPoPOClientCredentialsGrantRequestEntityConverter(
94-
parametersConverter, dPoPProofBuilder);
95-
96-
return new DefaultDPoPAccessTokenResponseClient(requestEntityConverter);
97-
}
98-
99104
@Bean
100105
public HelseIDDPoPClientCredentialTokenService helseIdApiDPOPClientCredentialTokenService(
101106
ClientRegistrationRepository clientRegistrationRepository,
102107
OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
103108
DPoPProofBuilder dPoPProofBuilder,
104-
DPoPAccessTokenResponseClient<DPoPClientCredentialsGrantRequest> credentialsGrantClient) {
109+
@Qualifier("authorizationCredentialsGrantResponseDpopClient")
110+
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>
111+
authorizationCredentialsGrantResponseDpopClient) {
105112
ClientRegistration clientRegistration =
106113
clientRegistrationRepository.findByRegistrationId(HELSEID_CREDENTIALS);
114+
115+
ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider =
116+
new ClientCredentialsOAuth2AuthorizedClientProvider();
117+
clientCredentialsAuthorizedClientProvider.setAccessTokenResponseClient(
118+
authorizationCredentialsGrantResponseDpopClient);
119+
107120
return new HelseIDDPoPClientCredentialTokenService(
108121
clientRegistration,
109122
dPoPProofBuilder,
110123
oAuth2AuthorizedClientService,
111-
credentialsGrantClient,
124+
clientCredentialsAuthorizedClientProvider,
112125
Duration.ofMinutes(2));
113126
}
114127
}

0 commit comments

Comments
 (0)