lyh
3 天以前 3ce27b7faf8850d101a1511a685250fe562dca18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.lxzn.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
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;
 
import java.util.Arrays;
import java.util.Collections;
 
/**
 * @author joetao
 * swagger相关配置类
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig {
 
    //这个东西是项目的根路径,也就是“/oauth/token”前面的那一串
    //这个东西在配置文件里写的,大家也可以直接写死在配置文件中
    @Value("${auth_server}")
    private String AUTH_SERVER;
 
    private Contact contact = new Contact("灵秀智能",AUTH_SERVER + "/swagger-ui.html", "zhangherong@xalxzn.com");
 
    /**
     * 主要是这个方法,其他的方法是抽出去的,所以大家不要害怕为啥有这么多方法
     * 在 basePackage 里面写需要生成文档的 controller 路径
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lxzn"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()));
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("灵秀智能API文档 v1.0")
                .description("灵秀智能API文档")
                .contact(contact)
                .version("1.0")
                .build();
    }
 
    /**
     * 这个类决定了你使用哪种认证方式,我这里使用密码模式
     * 其他方式自己摸索一下,完全莫问题啊
     */
    private SecurityScheme securityScheme() {
        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(AUTH_SERVER + "/oauth/token");
 
        return new OAuthBuilder()
                .name("spring_oauth")
                .grantTypes(Collections.singletonList(grantType))
                .scopes(Arrays.asList(scopes()))
                .build();
    }
 
    /**
     * 这里设置 swagger2 认证的安全上下文
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference("spring_oauth", scopes())))
                .forPaths(PathSelectors.any())
                .build();
    }
 
    /**
     * 这里是写允许认证的scope
     */
    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{
                new AuthorizationScope("all", "All scope is trusted!")
        };
    }
}