|
@@ -0,0 +1,68 @@
|
|
|
+package cn.efunbox.audio.config;
|
|
|
+
|
|
|
+import cn.efunbox.audio.aop.AdminInterceptor;
|
|
|
+import cn.efunbox.audio.aop.AuthInterceptor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.EnvironmentAware;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
+import org.springframework.web.filter.CorsFilter;
|
|
|
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户权限验证拦截器
|
|
|
+ * Created by yao on 17-9-29.
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@EnableWebMvc
|
|
|
+public class Config extends WebMvcConfigurerAdapter{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接采用new interceptor或Autowired注入拦截器会导致dao为null的错误
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ AuthInterceptor authInterceptor(){
|
|
|
+ return new AuthInterceptor();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ AdminInterceptor adminInterceptor(){
|
|
|
+ return new AdminInterceptor();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addInterceptors(InterceptorRegistry registry) {
|
|
|
+ registry.addInterceptor(authInterceptor())
|
|
|
+ .addPathPatterns("/audio/search");
|
|
|
+
|
|
|
+ registry.addInterceptor(adminInterceptor())
|
|
|
+ .addPathPatterns("/**")
|
|
|
+ .excludePathPatterns("/device/**", "/admin/**", "/audio/search");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置跨域访问
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private CorsConfiguration buildConfig() {
|
|
|
+ CorsConfiguration corsConfiguration = new CorsConfiguration();
|
|
|
+ corsConfiguration.addAllowedOrigin("*"); // 1
|
|
|
+ corsConfiguration.addAllowedHeader("*"); // 2
|
|
|
+ corsConfiguration.addAllowedMethod("*"); // 3
|
|
|
+ return corsConfiguration;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CorsFilter corsFilter() {
|
|
|
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
+ source.registerCorsConfiguration("/**", buildConfig()); // 4
|
|
|
+ return new CorsFilter(source);
|
|
|
+ }
|
|
|
+}
|