Move AOT hints to main module

Closes gh-1446
This commit is contained in:
Josh Long
2023-11-14 16:01:53 -08:00
committed by Joe Grandja
parent d7f8a16d94
commit 0c4df7f419
8 changed files with 358 additions and 265 deletions

View File

@@ -2,6 +2,7 @@ plugins {
id "org.springframework.boot" version "3.1.0"
id "io.spring.dependency-management" version "1.1.0"
id "java"
id 'org.graalvm.buildtools.native' version '0.9.27'
}
group = project.rootProject.group

View File

@@ -15,13 +15,17 @@
*/
package sample;
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import sample.web.AuthorizationConsentController;
/**
* @author Joe Grandja
* @author Josh Long
* @since 1.1
*/
@RegisterReflectionForBinding(AuthorizationConsentController.ScopeWithDescription.class)
@SpringBootApplication
public class DemoAuthorizationServerApplication {

View File

@@ -1,172 +0,0 @@
/*
* Copyright 2020-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.aot.hint;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import org.thymeleaf.expression.Lists;
import sample.web.AuthorizationConsentController;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.jackson2.CoreJackson2Module;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.client.jackson2.OAuth2ClientJackson2Module;
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module;
import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.jackson2.WebServletJackson2Module;
/**
* {@link RuntimeHintsRegistrar} that registers {@link RuntimeHints} required for the sample.
* Statically registered via META-INF/spring/aot.factories.
*
* @author Joe Grandja
* @since 1.2
*/
class DemoAuthorizationServerRuntimeHints implements RuntimeHintsRegistrar {
private final BindingReflectionHintsRegistrar reflectionHintsRegistrar = new BindingReflectionHintsRegistrar();
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
// Thymeleaf
hints.reflection().registerTypes(
Arrays.asList(
TypeReference.of(AuthorizationConsentController.ScopeWithDescription.class),
TypeReference.of(Lists.class)
), builder ->
builder.withMembers(MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)
);
// Collections -> UnmodifiableSet, UnmodifiableList, UnmodifiableMap, UnmodifiableRandomAccessList, etc.
hints.reflection().registerType(
Collections.class, MemberCategory.DECLARED_CLASSES);
// HashSet
hints.reflection().registerType(
HashSet.class, MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS);
// Spring Security and Spring Authorization Server
hints.reflection().registerTypes(
Arrays.asList(
TypeReference.of(AbstractAuthenticationToken.class),
TypeReference.of(WebAuthenticationDetails.class),
TypeReference.of(UsernamePasswordAuthenticationToken.class),
TypeReference.of(User.class),
TypeReference.of(OAuth2AuthenticationToken.class),
TypeReference.of(DefaultOidcUser.class),
TypeReference.of(DefaultOAuth2User.class),
TypeReference.of(OidcUserAuthority.class),
TypeReference.of(OAuth2UserAuthority.class),
TypeReference.of(SimpleGrantedAuthority.class),
TypeReference.of(OidcIdToken.class),
TypeReference.of(AbstractOAuth2Token.class),
TypeReference.of(OidcUserInfo.class),
TypeReference.of(OAuth2AuthorizationRequest.class),
TypeReference.of(AuthorizationGrantType.class),
TypeReference.of(OAuth2AuthorizationResponseType.class),
TypeReference.of(OAuth2TokenFormat.class)
), builder ->
builder.withMembers(MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)
);
// Jackson Modules - Spring Security and Spring Authorization Server
hints.reflection().registerTypes(
Arrays.asList(
TypeReference.of(CoreJackson2Module.class),
TypeReference.of(WebServletJackson2Module.class),
TypeReference.of(OAuth2ClientJackson2Module.class),
TypeReference.of(OAuth2AuthorizationServerJackson2Module.class)
), builder ->
builder.withMembers(MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)
);
// Jackson Mixins - Spring Security and Spring Authorization Server
try {
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.UnmodifiableSetMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.UnmodifiableListMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.UnmodifiableMapMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.server.authorization.jackson2.UnmodifiableMapMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.server.authorization.jackson2.HashSetMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.web.jackson2.WebAuthenticationDetailsMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.UsernamePasswordAuthenticationTokenMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.UserMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.OAuth2AuthenticationTokenMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.DefaultOidcUserMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.DefaultOAuth2UserMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.OidcUserAuthorityMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.OAuth2UserAuthorityMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.jackson2.SimpleGrantedAuthorityMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.OidcIdTokenMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.client.jackson2.OidcUserInfoMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationRequestMixin"));
this.reflectionHintsRegistrar.registerReflectionHints(hints.reflection(),
Class.forName("org.springframework.security.oauth2.server.authorization.jackson2.OAuth2TokenFormatMixin"));
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
// Sql Schema Resources
hints.resources().registerPattern(
"org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql");
hints.resources().registerPattern(
"org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql");
hints.resources().registerPattern(
"org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql");
}
}

View File

@@ -1,2 +0,0 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
sample.aot.hint.DemoAuthorizationServerRuntimeHints