|
|
@@ -0,0 +1,92 @@
|
|
|
+package com.uas.sso.sso.backend.config;
|
|
|
+
|
|
|
+import com.uas.sso.web.AccountConfigurer;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Properties;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Profile;
|
|
|
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Spring MVC web configurations which is used to intercept protected
|
|
|
+ * resources visit.
|
|
|
+ *
|
|
|
+ * @author huxz
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class SecurityConfiguration extends WebMvcConfigurerAdapter {
|
|
|
+
|
|
|
+ private static final Logger logger = Logger.getLogger(SecurityConfiguration.class);
|
|
|
+
|
|
|
+ private final ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private final SecurityInterceptor securityInterceptor;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public SecurityConfiguration(ApplicationContext applicationContext, SecurityInterceptor securityInterceptor) {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ this.securityInterceptor = securityInterceptor;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addInterceptors(InterceptorRegistry registry) {
|
|
|
+ registry.addInterceptor(securityInterceptor)
|
|
|
+ .addPathPatterns("/**");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addViewControllers(ViewControllerRegistry registry) {
|
|
|
+ registry.addViewController("/").setViewName("index");
|
|
|
+ registry.addViewController("/index").setViewName("index");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "accountConfigurer")
|
|
|
+ @Profile(value = {"dev"})
|
|
|
+ public AccountConfigurer devAccountConfigurer() {
|
|
|
+ return initAccountConfigurer(applicationContext, "classpath:config/account-dev.properties");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "accountConfigurer")
|
|
|
+ @Profile(value = {"test"})
|
|
|
+ public AccountConfigurer testAccountConfigurer() {
|
|
|
+ return initAccountConfigurer(applicationContext, "classpath:config/account-test.properties");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "accountConfigurer")
|
|
|
+ @Profile(value = {"prod"})
|
|
|
+ public AccountConfigurer prodAccountConfigurer() {
|
|
|
+ return initAccountConfigurer(applicationContext, "classpath:config/account-prod.properties");
|
|
|
+ }
|
|
|
+
|
|
|
+ private AccountConfigurer initAccountConfigurer(ApplicationContext applicationContext, String classpath) {
|
|
|
+ AccountConfigurer configurer = new AccountConfigurer();
|
|
|
+
|
|
|
+ // 解决Spring Boot应用不支持Resource.getFile方式读取配置文件的问题
|
|
|
+ Properties prop = getProperties(applicationContext, classpath);
|
|
|
+
|
|
|
+ if (prop != null) {
|
|
|
+ configurer.initProperties(prop);
|
|
|
+ } else {
|
|
|
+ throw new NullPointerException("Initializing is not available AccountConfigLocation on the classpath");
|
|
|
+ }
|
|
|
+ return configurer;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Properties getProperties(ApplicationContext applicationContext, String location) {
|
|
|
+ Properties prop;
|
|
|
+ try (InputStream stream = applicationContext.getResource(location).getInputStream()) {
|
|
|
+ prop = new Properties();
|
|
|
+ prop.load(stream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IllegalStateException("account read config file error", e);
|
|
|
+ }
|
|
|
+ return prop;
|
|
|
+ }
|
|
|
+}
|