Skip to content

Commit abce2eb

Browse files
Add native-image support for Core classes
Closes gh-2104
1 parent dc2e93f commit abce2eb

File tree

10 files changed

+564
-0
lines changed

10 files changed

+564
-0
lines changed

Diff for: gradle/dependency-management.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencyManagement {
3232
dependencySet(group: 'org.mockito', version: '4.2.0') {
3333
entry 'mockito-core'
3434
entry 'mockito-junit-jupiter'
35+
entry 'mockito-inline'
3536
}
3637

3738
dependencySet(group: 'org.mongodb', version: '4.6.0') {

Diff for: spring-session-core/spring-session-core.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919

2020
testImplementation "io.projectreactor:reactor-test"
2121
testImplementation "org.mockito:mockito-core"
22+
testImplementation "org.mockito:mockito-inline"
2223
testImplementation "edu.umd.cs.mtc:multithreadedtc"
2324
testImplementation "org.springframework:spring-test"
2425
testImplementation "org.assertj:assertj-core"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2014-2022 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+
17+
package org.springframework.session.aot.hint;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.TreeSet;
22+
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.security.authentication.AbstractAuthenticationToken;
27+
import org.springframework.security.authentication.AccountExpiredException;
28+
import org.springframework.security.authentication.AuthenticationServiceException;
29+
import org.springframework.security.authentication.BadCredentialsException;
30+
import org.springframework.security.authentication.CredentialsExpiredException;
31+
import org.springframework.security.authentication.DisabledException;
32+
import org.springframework.security.authentication.InsufficientAuthenticationException;
33+
import org.springframework.security.authentication.LockedException;
34+
import org.springframework.security.authentication.ProviderNotFoundException;
35+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
36+
import org.springframework.security.core.AuthenticationException;
37+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
38+
import org.springframework.security.core.context.SecurityContextImpl;
39+
import org.springframework.security.core.userdetails.User;
40+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
41+
42+
/**
43+
* A {@link RuntimeHintsRegistrar} for common session security hints.
44+
*
45+
* @author Marcus Da Coregio
46+
*/
47+
public class CommonSessionSecurityHints implements RuntimeHintsRegistrar {
48+
49+
@Override
50+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
51+
Arrays.asList(TypeReference.of(String.class), TypeReference.of(ArrayList.class),
52+
TypeReference.of(TreeSet.class), TypeReference.of(SecurityContextImpl.class),
53+
TypeReference.of(SimpleGrantedAuthority.class), TypeReference.of(User.class),
54+
TypeReference.of(Number.class), TypeReference.of(Long.class), TypeReference.of(Integer.class),
55+
TypeReference.of(AbstractAuthenticationToken.class),
56+
TypeReference.of(UsernamePasswordAuthenticationToken.class), TypeReference.of(StackTraceElement.class),
57+
TypeReference.of(Throwable.class), TypeReference.of(Exception.class),
58+
TypeReference.of(RuntimeException.class), TypeReference.of(AuthenticationException.class),
59+
TypeReference.of(BadCredentialsException.class), TypeReference.of(UsernameNotFoundException.class),
60+
TypeReference.of(AccountExpiredException.class), TypeReference.of(ProviderNotFoundException.class),
61+
TypeReference.of(DisabledException.class), TypeReference.of(LockedException.class),
62+
TypeReference.of(AuthenticationServiceException.class),
63+
TypeReference.of(CredentialsExpiredException.class),
64+
TypeReference.of(InsufficientAuthenticationException.class),
65+
TypeReference
66+
.of("org.springframework.security.web.authentication.session.SessionAuthenticationException"),
67+
TypeReference.of(
68+
"org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException"),
69+
TypeReference.of("java.util.Collections$UnmodifiableCollection"),
70+
TypeReference.of("java.util.Collections$UnmodifiableList"),
71+
TypeReference.of("java.util.Collections$EmptyList"),
72+
TypeReference.of("java.util.Collections$UnmodifiableRandomAccessList"),
73+
TypeReference.of("java.util.Collections$UnmodifiableSet"),
74+
TypeReference.of("org.springframework.security.core.userdetails.User$AuthorityComparator"))
75+
.forEach(hints.serialization()::registerType);
76+
registerOAuth2ClientHintsIfNeeded(hints);
77+
registerOAuth2ResourceServerHintsIfNeeded(hints);
78+
}
79+
80+
private void registerOAuth2ResourceServerHintsIfNeeded(RuntimeHints hints) {
81+
Arrays.asList(
82+
TypeReference.of("org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken"),
83+
TypeReference.of(
84+
"org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken"),
85+
TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException"))
86+
.forEach((type) -> hints.serialization().registerType(type, (hint) -> hint.onReachableType(TypeReference
87+
.of("org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken"))));
88+
}
89+
90+
private void registerOAuth2ClientHintsIfNeeded(RuntimeHints hints) {
91+
Arrays.asList(
92+
TypeReference.of("org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken"),
93+
TypeReference
94+
.of("org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken"),
95+
TypeReference.of(
96+
"org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken"),
97+
TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException"))
98+
.forEach((type) -> hints.serialization().registerType(type, (hint) -> hint.onReachableType(TypeReference
99+
.of("org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken"))));
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-2022 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+
17+
package org.springframework.session.aot.hint.server;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
import org.springframework.security.web.server.csrf.DefaultCsrfToken;
22+
import org.springframework.util.ClassUtils;
23+
24+
/**
25+
* {@link RuntimeHintsRegistrar} for Reactive Session hints.
26+
*
27+
* @author Marcus Da Coregio
28+
*/
29+
public class WebSessionSecurityHints implements RuntimeHintsRegistrar {
30+
31+
@Override
32+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
33+
if (!ClassUtils.isPresent("org.springframework.web.server.WebSession", classLoader) || !ClassUtils
34+
.isPresent("org.springframework.security.web.server.csrf.DefaultCsrfToken", classLoader)) {
35+
return;
36+
}
37+
hints.serialization().registerType(DefaultCsrfToken.class);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014-2022 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+
17+
package org.springframework.session.aot.hint.servlet;
18+
19+
import java.util.Arrays;
20+
import java.util.Locale;
21+
import java.util.TreeMap;
22+
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.security.web.authentication.WebAuthenticationDetails;
27+
import org.springframework.security.web.csrf.DefaultCsrfToken;
28+
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
29+
import org.springframework.security.web.savedrequest.SavedCookie;
30+
import org.springframework.util.ClassUtils;
31+
32+
/**
33+
* {@link RuntimeHintsRegistrar} for Servlet Session hints.
34+
*
35+
* @author Marcus Da Coregio
36+
*/
37+
public class HttpSessionSecurityHints implements RuntimeHintsRegistrar {
38+
39+
@Override
40+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
41+
if (!ClassUtils.isPresent("jakarta.servlet.http.HttpSession", classLoader)
42+
|| !ClassUtils.isPresent("org.springframework.security.web.csrf.DefaultCsrfToken", classLoader)) {
43+
return;
44+
}
45+
Arrays.asList(TypeReference.of(TreeMap.class), TypeReference.of(Locale.class),
46+
TypeReference.of(DefaultSavedRequest.class), TypeReference.of(DefaultCsrfToken.class),
47+
TypeReference.of(WebAuthenticationDetails.class), TypeReference.of(SavedCookie.class),
48+
TypeReference.of("java.lang.String$CaseInsensitiveComparator"))
49+
.forEach(hints.serialization()::registerType);
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.session.aot.hint.CommonSessionSecurityHints,\
3+
org.springframework.session.aot.hint.servlet.HttpSessionSecurityHints,\
4+
org.springframework.session.aot.hint.server.WebSessionSecurityHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2014-2022 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+
17+
package org.springframework.session.aot.hint;
18+
19+
import java.util.ArrayList;
20+
import java.util.TreeSet;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
27+
import org.springframework.aot.hint.RuntimeHints;
28+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
29+
import org.springframework.aot.hint.TypeReference;
30+
import org.springframework.core.io.support.SpringFactoriesLoader;
31+
import org.springframework.security.authentication.AbstractAuthenticationToken;
32+
import org.springframework.security.authentication.AccountExpiredException;
33+
import org.springframework.security.authentication.AuthenticationServiceException;
34+
import org.springframework.security.authentication.BadCredentialsException;
35+
import org.springframework.security.authentication.CredentialsExpiredException;
36+
import org.springframework.security.authentication.DisabledException;
37+
import org.springframework.security.authentication.InsufficientAuthenticationException;
38+
import org.springframework.security.authentication.LockedException;
39+
import org.springframework.security.authentication.ProviderNotFoundException;
40+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
41+
import org.springframework.security.core.AuthenticationException;
42+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
43+
import org.springframework.security.core.context.SecurityContextImpl;
44+
import org.springframework.security.core.userdetails.User;
45+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
46+
47+
import static org.assertj.core.api.Assertions.assertThat;
48+
49+
/**
50+
* Tests for {@link CommonSessionSecurityHints}
51+
*
52+
* @author Marcus Da Coregio
53+
*/
54+
class CommonSessionSecurityHintsTests {
55+
56+
private final RuntimeHints hints = new RuntimeHints();
57+
58+
private final CommonSessionSecurityHints commonSessionSecurityHints = new CommonSessionSecurityHints();
59+
60+
@ParameterizedTest
61+
@MethodSource("getSerializationHintTypes")
62+
void coreTypesHasHints(TypeReference typeReference) {
63+
this.commonSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
64+
assertThat(new SerializationHintsPredicates().onType(typeReference)).accepts(this.hints);
65+
}
66+
67+
@Test
68+
void aotFactoriesContainsRegistrar() {
69+
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
70+
.load(RuntimeHintsRegistrar.class).stream()
71+
.anyMatch((registrar) -> registrar instanceof CommonSessionSecurityHints);
72+
assertThat(match).isTrue();
73+
}
74+
75+
private static Stream<TypeReference> getSerializationHintTypes() {
76+
return Stream.of(TypeReference.of(String.class), TypeReference.of(ArrayList.class),
77+
TypeReference.of(TreeSet.class), TypeReference.of(SecurityContextImpl.class),
78+
TypeReference.of(SimpleGrantedAuthority.class), TypeReference.of(User.class),
79+
TypeReference.of(Number.class), TypeReference.of(Long.class), TypeReference.of(Integer.class),
80+
TypeReference.of(AbstractAuthenticationToken.class),
81+
TypeReference.of(UsernamePasswordAuthenticationToken.class), TypeReference.of(StackTraceElement.class),
82+
TypeReference.of(Throwable.class), TypeReference.of(Exception.class),
83+
TypeReference.of(RuntimeException.class), TypeReference.of(AuthenticationException.class),
84+
TypeReference.of(BadCredentialsException.class), TypeReference.of(UsernameNotFoundException.class),
85+
TypeReference.of(AccountExpiredException.class), TypeReference.of(ProviderNotFoundException.class),
86+
TypeReference.of(DisabledException.class), TypeReference.of(LockedException.class),
87+
TypeReference.of(AuthenticationServiceException.class),
88+
TypeReference.of(CredentialsExpiredException.class),
89+
TypeReference.of(InsufficientAuthenticationException.class),
90+
TypeReference
91+
.of("org.springframework.security.web.authentication.session.SessionAuthenticationException"),
92+
TypeReference.of(
93+
"org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException"),
94+
TypeReference.of("java.util.Collections$UnmodifiableCollection"),
95+
TypeReference.of("java.util.Collections$UnmodifiableList"),
96+
TypeReference.of("java.util.Collections$EmptyList"),
97+
TypeReference.of("java.util.Collections$UnmodifiableRandomAccessList"),
98+
TypeReference.of("java.util.Collections$UnmodifiableSet"),
99+
TypeReference.of("org.springframework.security.core.userdetails.User$AuthorityComparator"),
100+
TypeReference.of("org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken"),
101+
TypeReference.of(
102+
"org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken"),
103+
TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException"),
104+
TypeReference.of("org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken"),
105+
TypeReference
106+
.of("org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken"),
107+
TypeReference.of(
108+
"org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken"),
109+
TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException"));
110+
}
111+
112+
}

0 commit comments

Comments
 (0)