JCM 4 роки тому
батько
коміт
04eb3f5d6a

+ 62 - 0
common-springboot/src/main/java/com/gihon/common/swagger/Knife4jConfig.java

@@ -0,0 +1,62 @@
+package com.gihon.common.swagger;
+
+import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.web.context.WebServerInitializedEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+
+/**
+ * Knife4j配置
+ */
+@Configuration
+@EnableSwagger2
+@EnableKnife4j
+@Import(BeanValidatorPluginsConfiguration.class)
+public class Knife4jConfig implements ApplicationListener<WebServerInitializedEvent> {
+
+    private static Logger logger = LoggerFactory.getLogger(Knife4jConfig.class);
+
+    @Bean(value = "web")
+    public Docket createRestAdmin() {
+        return new Docket(DocumentationType.SWAGGER_2)
+                .useDefaultResponseMessages(false)
+                .groupName("web")
+                .apiInfo(apiInfo())
+                .select()
+                .apis(RequestHandlerSelectors.basePackage("com.gihon.controller"))
+                .paths(PathSelectors.any())
+                .build();
+
+    }
+
+    private ApiInfo apiInfo() {
+        return new ApiInfoBuilder()
+                .title("权限验证")
+                .build();
+    }
+
+    @Override
+    public void onApplicationEvent(WebServerInitializedEvent event) {
+        try {
+            InetAddress inetAddress = Inet4Address.getLocalHost();
+            logger.info("\033[1;33m项目启动成功!接口文档地址: http://" + inetAddress.getHostAddress() + ":" + event.getWebServer().getPort() + event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path") + "/doc.html\033[0m");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 24 - 0
common-springboot/src/main/java/com/gihon/common/web/CorsConfig.java

@@ -0,0 +1,24 @@
+package com.gihon.common.web;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class CorsConfig implements WebMvcConfigurer {
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        // 允许跨域访问的路径
+        registry.addMapping("/**")
+                // 允许跨域访问的源
+                .allowedOrigins("*")
+                // 允许请求方法
+                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
+                // 预检间隔时间
+                .maxAge(168000)
+                // 允许头部设置
+                .allowedHeaders("*")
+                // 是否发送cookie
+                .allowCredentials(true);
+    }
+}

+ 1 - 1
common-springboot/src/main/java/com/gihon/configSecurity/config/MySecurityConfiguration.java

@@ -107,7 +107,7 @@ public class MySecurityConfiguration extends WebSecurityConfigurerAdapter {
                 .anyRequest().authenticated(); // 所有的请求地址,都必须登录才能访问。
 
         // 异常处理配置
-        http.exceptionHandling().accessDeniedHandler(new MyAccessDeniedHandler());
+//        http.exceptionHandling().accessDeniedHandler(new MyAccessDeniedHandler());
 
         // 配置rememberMe, 使用rememberMe功能的时候,认证成功结果跳转,一定要使用重定向。
         http.rememberMe()