org.zodiac.autoconfigure.security.oauth.AppResourceServerTokenRelayAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.security.oauth;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.zodiac.commons.constants.SystemPropertiesConstants;
@SpringBootConfiguration
@ConditionalOnWebApplication
@AutoConfigureAfter(value = {OAuth2AutoConfiguration.class})
@AppResourceServerTokenRelayAutoConfiguration.ConditionalOnOAuth2ClientInResourceServer
@ConditionalOnClass(value = {org.springframework.security.oauth2.client.OAuth2RestTemplate.class})
//@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_SECURITY_TOKEN_RELAY_ENABLED, matchIfMissing = true)
@ConditionalOnProperty(value = org.zodiac.security.oauth.constants.OAuthSystemPropertiesConstants.SPRING_SECURITY_OAUTH_TOKEN_RELAY_ENABLED, matchIfMissing = true)
public class AppResourceServerTokenRelayAutoConfiguration {
public AppResourceServerTokenRelayAutoConfiguration() {
super();
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.security.oauth.AppAccessTokenContextRelay accessTokenContextRelay(org.springframework.security.oauth2.client.OAuth2ClientContext context) {
return new org.zodiac.security.oauth.AppAccessTokenContextRelay(context);
}
@SpringBootConfiguration
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_SECURITY_TOKEN_RELAY_ENABLED, matchIfMissing = true)
public static class ResourceServerTokenRelayRegistrationAutoConfiguration implements WebMvcConfigurer {
private org.zodiac.security.oauth.AppAccessTokenContextRelay accessTokenContextRelay;
public ResourceServerTokenRelayRegistrationAutoConfiguration(org.zodiac.security.oauth.AppAccessTokenContextRelay accessTokenContextRelay) {
this.accessTokenContextRelay = accessTokenContextRelay;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(
new HandlerInterceptorAdapter() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
accessTokenContextRelay.copyToken();
return true;
}
}
);
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(value = {OAuth2OnClientInResourceServerCondition.class})
@interface ConditionalOnOAuth2ClientInResourceServer {
}
private static class OAuth2OnClientInResourceServerCondition extends AllNestedConditions {
OAuth2OnClientInResourceServerCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnBean(value = {org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration.class})
static class Server {
}
@ConditionalOnBean(OAuth2ClientConfiguration.class)
static class Client {
}
}
}