Swagger2+oauth2配置

该博客介绍了如何在项目中集成Springfox库来创建Swagger2 API文档,包括添加依赖、配置信息、定义认证方式和响应状态码,以便为HTTP方法提供统一的响应处理。此外,还展示了如何配置摘要信息,如API的标题,以及如何定义认证的scope。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 导入依赖
        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>3.0.0</version>
        </dependency>
  • 配置信息
  @Bean
    public Docket createRestApi() {
        List<Response> responses = new ArrayList<Response>() {{
            add(new ResponseBuilder().code("201")
                .description("创建成功")
                .build());
            add(new ResponseBuilder().code("202")
                .description("请求已受理,正在进一步处理")
                .build());
            add(new ResponseBuilder().code("204")
                .description("请求已成功,但无需更多描述信息")
                .build());
            add(new ResponseBuilder().code("400")
                .description("请求参数错误或格式不正确")
                .build());
            add(new ResponseBuilder().code("401")
                .description("Token 验证失败")
                .build());
            add(new ResponseBuilder().code("403")
                .description("登录失败或权限禁止")
                .build());
            add(new ResponseBuilder().code("417")
                .description("参数格式不正确")
                .build());
        }};
        return new Docket(DocumentationType.SWAGGER_2)
            .securitySchemes(Collections.singletonList(securitySchemes()))
            .securityContexts(Collections.singletonList(securityContexts()))
            .globalResponses(HttpMethod.GET, responses)
            .globalResponses(HttpMethod.POST, responses)
            .globalResponses(HttpMethod.PUT, responses)
            .globalResponses(HttpMethod.HEAD, responses)
            .globalResponses(HttpMethod.DELETE, responses)
            .select()
            .apis(RequestHandlerSelectors.basePackage("wlgzs.exam.controller"))
            .build()
            .apiInfo(apiInfo());
    }

    /**
     * 认证方式使用密码模式
     */
    private SecurityScheme securitySchemes() {
        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("/oauth/token");
        return new OAuthBuilder()
            .name("Authorization")
            .grantTypes(Collections.singletonList(grantType))
            .scopes(Arrays.asList(scopes()))
            .build();
    }

    /**
     * 设置 swagger2 认证的安全上下文
     */
    private SecurityContext securityContexts() {
        return SecurityContext.builder()
            .securityReferences(Collections.singletonList(new SecurityReference("Authorization", scopes())))
            .operationSelector((each) -> true)
            .build();
    }

    /**
     * 允许认证的scope
     */
    private AuthorizationScope[] scopes() {
        AuthorizationScope authorizationScope = new AuthorizationScope("all", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return authorizationScopes;
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("API在线考试文档")
            .build();
    }

在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

blog_xsong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值