在开发过程中遇到了,需要根据配置文件来控制是否开启单点的问题。单点cas需要在spring的启动类上进行注解,所以需要通过配置文件来进行控制是否引入。
@SpringBootApplication
@EnableEurekaClient
@EnableScheduling
@EnableCaching
@EnableCasClient //启用cas需要的
public class MyTestApplication {
public static void main(String[] args) {
SpringApplication.run(SidlServerAuthorityApplication.class, args);
}
}
方法:
通过condition来进行控制
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.stereotype.Component;
@Component
public class CasConditonal implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
boolean ssoEnable = Boolean.parseBoolean(conditionContext.getEnvironment().getProperty("sso.enable"));
return ssoEnable;
}
}
import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Conditional;
@Conditional(CasConditonal.class)
@SpringBootConfiguration
@EnableCasClient
public class CasConfig {
}
使用一段时间后发现了一个更简单的注解可以直接由配置文件控制
@ConditionalOnProperty(name = "sso.protocol", havingValue = "oauth2")
@SpringBootConfiguration
public class Oauth2Config {
@Bean
public FilterRegistrationBean filterSingleRegistration() {
final FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SingleSignOutFilter());
registration.addUrlPatterns("/sso/login");
registration.setOrder(0);
return registration;
}
}