|
|
@ -86,7 +86,8 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
return new ResourceServerFilterChainOrderProcessor(properties);
|
|
|
|
return new ResourceServerFilterChainOrderProcessor(properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static class ResourceSecurityConfigurer extends ResourceServerConfigurerAdapter {
|
|
|
|
protected static class ResourceSecurityConfigurer
|
|
|
|
|
|
|
|
extends ResourceServerConfigurerAdapter {
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceServerProperties resource;
|
|
|
|
private ResourceServerProperties resource;
|
|
|
|
|
|
|
|
|
|
|
@ -95,7 +96,8 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
|
|
|
|
public void configure(ResourceServerSecurityConfigurer resources)
|
|
|
|
|
|
|
|
throws Exception {
|
|
|
|
resources.resourceId(this.resource.getResourceId());
|
|
|
|
resources.resourceId(this.resource.getResourceId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -110,26 +112,32 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
implements BeanPostProcessor, ApplicationContextAware {
|
|
|
|
implements BeanPostProcessor, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
|
|
private final ResourceServerProperties properties;
|
|
|
|
private final ResourceServerProperties properties;
|
|
|
|
|
|
|
|
|
|
|
|
private ApplicationContext context;
|
|
|
|
private ApplicationContext context;
|
|
|
|
|
|
|
|
|
|
|
|
private ResourceServerFilterChainOrderProcessor(ResourceServerProperties properties) {
|
|
|
|
private ResourceServerFilterChainOrderProcessor(
|
|
|
|
|
|
|
|
ResourceServerProperties properties) {
|
|
|
|
this.properties = properties;
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
|
|
|
public void setApplicationContext(ApplicationContext context)
|
|
|
|
|
|
|
|
throws BeansException {
|
|
|
|
this.context = context;
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
|
|
|
|
|
|
|
throws BeansException {
|
|
|
|
return bean;
|
|
|
|
return bean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName)
|
|
|
|
|
|
|
|
throws BeansException {
|
|
|
|
if (bean instanceof ResourceServerConfiguration) {
|
|
|
|
if (bean instanceof ResourceServerConfiguration) {
|
|
|
|
if (this.context.getBeanNamesForType(ResourceServerConfiguration.class, false, false).length == 1) {
|
|
|
|
if (this.context.getBeanNamesForType(ResourceServerConfiguration.class,
|
|
|
|
|
|
|
|
false, false).length == 1) {
|
|
|
|
ResourceServerConfiguration config = (ResourceServerConfiguration) bean;
|
|
|
|
ResourceServerConfiguration config = (ResourceServerConfiguration) bean;
|
|
|
|
config.setOrder(this.properties.getFilterOrder());
|
|
|
|
config.setOrder(this.properties.getFilterOrder());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -139,10 +147,12 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static class ResourceServerCondition extends SpringBootCondition implements ConfigurationCondition {
|
|
|
|
protected static class ResourceServerCondition extends SpringBootCondition
|
|
|
|
|
|
|
|
implements ConfigurationCondition {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String AUTHORIZATION_ANNOTATION = "org.springframework."
|
|
|
|
private static final String AUTHORIZATION_ANNOTATION = "org.springframework."
|
|
|
|
+ "security.oauth2.config.annotation.web.configuration." + "AuthorizationServerEndpointsConfiguration";
|
|
|
|
+ "security.oauth2.config.annotation.web.configuration."
|
|
|
|
|
|
|
|
+ "AuthorizationServerEndpointsConfiguration";
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ConfigurationPhase getConfigurationPhase() {
|
|
|
|
public ConfigurationPhase getConfigurationPhase() {
|
|
|
@ -150,37 +160,47 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
|
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
|
|
|
ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth ResourceServer Condition");
|
|
|
|
AnnotatedTypeMetadata metadata) {
|
|
|
|
|
|
|
|
ConditionMessage.Builder message = ConditionMessage
|
|
|
|
|
|
|
|
.forCondition("OAuth ResourceServer Condition");
|
|
|
|
Environment environment = context.getEnvironment();
|
|
|
|
Environment environment = context.getEnvironment();
|
|
|
|
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, "security.oauth2.resource.");
|
|
|
|
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
|
|
|
|
|
|
|
|
"security.oauth2.resource.");
|
|
|
|
if (hasOAuthClientId(environment)) {
|
|
|
|
if (hasOAuthClientId(environment)) {
|
|
|
|
return ConditionOutcome.match(message.foundExactly("client-id property"));
|
|
|
|
return ConditionOutcome.match(message.foundExactly("client-id property"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!resolver.getSubProperties("jwt").isEmpty()) {
|
|
|
|
if (!resolver.getSubProperties("jwt").isEmpty()) {
|
|
|
|
return ConditionOutcome.match(message.foundExactly("JWT resource configuration"));
|
|
|
|
return ConditionOutcome
|
|
|
|
|
|
|
|
.match(message.foundExactly("JWT resource configuration"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!resolver.getSubProperties("jwk").isEmpty()) {
|
|
|
|
if (!resolver.getSubProperties("jwk").isEmpty()) {
|
|
|
|
return ConditionOutcome
|
|
|
|
return ConditionOutcome
|
|
|
|
.match(message.foundExactly("JWK resource configuration"));
|
|
|
|
.match(message.foundExactly("JWK resource configuration"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (StringUtils.hasText(resolver.getProperty("user-info-uri"))) {
|
|
|
|
if (StringUtils.hasText(resolver.getProperty("user-info-uri"))) {
|
|
|
|
return ConditionOutcome.match(message.foundExactly("user-info-uri property"));
|
|
|
|
return ConditionOutcome
|
|
|
|
|
|
|
|
.match(message.foundExactly("user-info-uri property"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (StringUtils.hasText(resolver.getProperty("token-info-uri"))) {
|
|
|
|
if (StringUtils.hasText(resolver.getProperty("token-info-uri"))) {
|
|
|
|
return ConditionOutcome.match(message.foundExactly("token-info-uri property"));
|
|
|
|
return ConditionOutcome
|
|
|
|
|
|
|
|
.match(message.foundExactly("token-info-uri property"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ClassUtils.isPresent(AUTHORIZATION_ANNOTATION, null)) {
|
|
|
|
if (ClassUtils.isPresent(AUTHORIZATION_ANNOTATION, null)) {
|
|
|
|
if (AuthorizationServerEndpointsConfigurationBeanCondition.matches(context)) {
|
|
|
|
if (AuthorizationServerEndpointsConfigurationBeanCondition
|
|
|
|
return ConditionOutcome.match(message.found("class").items(AUTHORIZATION_ANNOTATION));
|
|
|
|
.matches(context)) {
|
|
|
|
|
|
|
|
return ConditionOutcome.match(
|
|
|
|
|
|
|
|
message.found("class").items(AUTHORIZATION_ANNOTATION));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ConditionOutcome
|
|
|
|
return ConditionOutcome.noMatch(
|
|
|
|
.noMatch(message.didNotFind("client id, JWT resource or authorization server").atAll());
|
|
|
|
message.didNotFind("client id, JWT resource or authorization server")
|
|
|
|
|
|
|
|
.atAll());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean hasOAuthClientId(Environment environment) {
|
|
|
|
private boolean hasOAuthClientId(Environment environment) {
|
|
|
|
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, "security.oauth2.client.");
|
|
|
|
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
|
|
|
|
|
|
|
|
"security.oauth2.client.");
|
|
|
|
return StringUtils.hasLength(resolver.getProperty("client-id", ""));
|
|
|
|
return StringUtils.hasLength(resolver.getProperty("client-id", ""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -191,7 +211,8 @@ public class OAuth2ResourceServerConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean matches(ConditionContext context) {
|
|
|
|
public static boolean matches(ConditionContext context) {
|
|
|
|
Class<AuthorizationServerEndpointsConfigurationBeanCondition> type = AuthorizationServerEndpointsConfigurationBeanCondition.class;
|
|
|
|
Class<AuthorizationServerEndpointsConfigurationBeanCondition> type = AuthorizationServerEndpointsConfigurationBeanCondition.class;
|
|
|
|
Conditional conditional = AnnotationUtils.findAnnotation(type, Conditional.class);
|
|
|
|
Conditional conditional = AnnotationUtils.findAnnotation(type,
|
|
|
|
|
|
|
|
Conditional.class);
|
|
|
|
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(type);
|
|
|
|
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(type);
|
|
|
|
for (Class<? extends Condition> conditionType : conditional.value()) {
|
|
|
|
for (Class<? extends Condition> conditionType : conditional.value()) {
|
|
|
|
Condition condition = BeanUtils.instantiateClass(conditionType);
|
|
|
|
Condition condition = BeanUtils.instantiateClass(conditionType);
|
|
|
|