1、Swagger2 配置类

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

@Configuration
@EnableSwagger2
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("Authorization", authorizationScopes));
        return securityReferences;
    }

	// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 页面标题1
				.title("易电云平台RESTful API")
				// 描述
				.description("易电云平台").termsOfServiceUrl("http://localhost:8080/")
				// 创建人
				.contact(new Contact("Presoft", "http://www.presoft.com.cn", ""))
				// 版本号
				.version("1.0.1").build();
	}
}

2、访问拦截配置

注意要 implements  WebMvcConfigurer 接口,extends   WebMvcConfigurationSupport 不好使

@Configuration
public class InterceptorConfig  implements WebMvcConfigurer {
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/fileUpdataApi/upload","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }
	
}

 

3、红框内添加token,保存之后,每次访问header中都会带全局token

 原文

https://www.jianshu.com/p/07a6d2ac9fed

https://www.jianshu.com/p/7a24d202b395

https://blog.csdn.net/pengdandezhi/article/details/81182701

 

GitHub 加速计划 / sp / spring-boot
73.97 K
40.4 K
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:9 天前 )
d3a2bf40 * pr/42289: Add common definition annotations support for ConfigurationProperties Closes gh-42289 5 天前
44be2e11 Update `` to ensure that common bean definition annotations, such as `@Lazy`, `@Primary` and `@Fallback`, are applied. See gh-42289 5 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐