springmvc swagger2 设置全局token,解决接口需要token验证的问题

1、Swagger2 配置类

关键是在securitySchemes()方法配置里增加需要token的配置。
配置完成后,swagger-ui.html里右上角会有一个Authorize的按钮,录入该token即能成功调用相关接口

package com.lgom.Swagger;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.lgom.**.control")
public class SwaggerConfig {
	@Bean
	public Docket swaggerSpringMvcPlugin() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build()
				.securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
	}
	private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList= new ArrayList<ApiKey>();
        apiKeyList.add(new ApiKey("token", "token", "header"));
        return apiKeyList;
    }
 
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }
 
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences=new ArrayList<>();
        securityReferences.add(new SecurityReference("token", authorizationScopes));
        return securityReferences;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("MDS4000项目接口文档")
                .description("MDS4000项目接口测试")
                .version("V1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}

2、访问拦截配置

注意要 在Springmvc配置文件内,配置好不需要拦截的路径

<mvc:interceptors>
	    <mvc:interceptor>
	    <!-- 默认拦截所有请求 -->
	      <mvc:mapping path="/**"/>
	      <!-- 登录接口不拦截 -->
	      <mvc:exclude-mapping path="/sys/login" />
	      <!-- Swagger2相关不拦截 -->
	      <mvc:exclude-mapping path="/fileUpdataApi/upload" />
	      <mvc:exclude-mapping path="/swagger-resources/**" />
	      <mvc:exclude-mapping path="/webjars/**" />
	      <mvc:exclude-mapping path="/v2/**" />
	      <mvc:exclude-mapping path="/swagger-ui.html/**" />
	      <bean class="com.lgom.Filter.AppInterceptor"/>
	    </mvc:interceptor>
	</mvc:interceptors>

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注