JavaWeb微服务

Swagger2 集成 Oauth2

2019-04-04  本文已影响405人  码道功臣

为什么要给Swagger2集成Oauth2

LIVE DEMO

http://hiauth.cn/hiauth/swagger-ui.html

源码

https://github.com/bestaone/HiAuth

图例

1.png 2.png 3.png

步骤

建一个Springboot项目,并加上swagger2的依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

设置swagger2的基本配置

创建配置类Swagger2Config.java

@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger2", name = "auth.clientId")
public class Swagger2Config {

    private static final String VERSION = "1.0";
    private String title = "系统接口文档";
    private String description = "提供系统接口文档";
    private String basePackage = "com.bestaone.hiauth";

    @Value("${swagger2.auth.clientId:}")
    private String clientId;
    @Value("${swagger2.auth.clientSecret:}")
    private String clientSecret;
    @Value("${swagger2.auth.authorizationUri:}")
    private String authorizationUri;
    @Value("${swagger2.auth.tokenUri:}")
    private String tokenUri;
    @Value("${swagger2.auth.scopes:}")
    private String scopes;

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(VERSION).build();
    }

    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any()).build()
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()));
    }

}

设置swagger2的Oahuth2配置

修改配置类Swagger2Config.java,添加如下类容


    private SecurityScheme securityScheme() {
        return new OAuthBuilder()
                .name("OAuth2")
                .grantTypes(grantTypes())
                .scopes(Arrays.asList(scopes()))
                .build();
    }

    /**
     * 设置 swagger2 认证的安全上下文
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference("oauth2", scopes())))
                .forPaths(PathSelectors.any())
                .build();
    }

    /**
     * 设置认证的scope
     * @return
     */
    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{
                new AuthorizationScope("ALL", "All scope!")
        };
    }

    /**
     *  使用密码模式
     */
    @Bean
    List<GrantType> grantTypes() {
        List<GrantType> grantTypes = new ArrayList<>();
        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(tokenUri);
        grantTypes.add(grantType );
        return grantTypes;
    }

上一篇下一篇

猜你喜欢

热点阅读