【笔记】Spring整合Swagger

api-swagger

启动Tomcat,访问:http://localhost:8090

效果图

这里写图片描述
实现步骤

1.添加Maven依赖

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

2.下载swagger-ui
解压zip -> 复制dist文件 -> 重命名swagger-ui -> 放到webapp/static/plug目录下

3.自定义SwaggerConfig类

/** 
 * The Apache License 2.0
 * Copyright (c) 2018 sep6th
 * @see http://springfox.github.io/springfox/docs/current/#configuring-springfox
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig{
    @Bean
    public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .produces(newHashSet(MediaType.APPLICATION_JSON_VALUE))
                    .protocols(Sets.newHashSet("http"/* , "https" */))
                    .forCodeGeneration(true)
                    .select()
                    .apis(RequestHandlerSelectors.any()) // 扫描所有路径下的api文档
                    .paths(paths()) // 筛选路径,生成API文档
                    .build();
    }
    /**
     * api基本信息
     */
    private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("API-Swagger项目 API文档 By sep6th")    // 标题
                    .description("API-Swagger项目有两个业务模块,学校管理和学生管理!")    // 描述
                    .license("Apache License 2.0")
                    .version("1.1.0")    // api 版本号
                    .build();
    }

    /**
     * 筛选路径
     */
    private Predicate<String> paths() {
            return Predicates.or(
                    PathSelectors.regex("/student.*"),
                    PathSelectors.regex("/school.*"));
      }
}

4.MVC配置

    <!-- 配置扫描器, 使得@Controller注解生效 -->
    <context:component-scan base-package="com.sep6th.*.controller">
        <!-- 用swagger页面操作api时,不配置报406 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 配置扫描器, 生成swagger-Api页面 -->
    <context:component-scan base-package="springfox.documentation.swagger2"/>
    <!-- 配置扫描器, 必须在MVC配置文件里,扫描自定义swagger配置 -->
    <context:component-scan base-package="com.sep6th.base.config"/>

    <mvc:annotation-driven />

    <!-- 处理静态资源被“/”所拦截的问题 -->
    <mvc:default-servlet-handler />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

5.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>API DOCS</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="static/plug/swagger-ui/swagger-ui.css" >
<link rel="icon" type="image/png" href="static/plug/swagger-ui/favicon-32x32.png" />
<link rel="icon" type="image/png" href="static/plug/swagger-ui/favicon-16x16.png" />
<style>
html {
  box-sizing: border-box;
  overflow: -moz-scrollbars-vertical;
  overflow-y: scroll;
}
*, *:before, *:after {
  box-sizing: inherit;
}
body {
  margin:0;
  background: #fafafa;
}
</style>
</head>
<body>
    <div id="swagger-ui"></div>
</body>
<script src="static/plug/swagger-ui/swagger-ui-bundle.js"> </script>
<script src="static/plug/swagger-ui/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function(){
    const ui = SwaggerUIBundle({
    url: "http://127.0.0.1:8090/v2/api-docs",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })
  window.ui = ui;
}
</script>
</html>

6.Controller

@Controller
public class ApiController {

    /**
     * 显示API 文档
     */
    @RequestMapping("/api")
    public String api(){
        return "api-docs";
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security整合Swagger的过程中,我们需要进行以下几个步骤: 1. 配置Spring Security:在Spring Security配置类中,我们可以使用`WebSecurityConfigurerAdapter`来配置权限过滤和访问控制。可以在`configure(HttpSecurity http)`方法中添加`.antMatchers("/swagger-ui.html").permitAll()`来允许Swagger UI页面的访问。这样,Swagger UI页面将不会被Spring Security拦截。 2. 配置静态资源:Swagger UI页面需要访问一些静态资源,例如Swagger API文档和UI配置文件。我们可以在Spring Security配置类中使用`WebSecurity.configure(WebSecurity web)`方法来配置这些静态资源的访问权限。可以使用`web.ignoring().antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html")`来允许这些静态资源的访问。 3. 添加相关依赖:在项目的pom.xml文件中,我们需要添加Spring Security和Swagger的相关依赖。可以添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <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> ``` 这些依赖将帮助我们实现Spring Security和Swagger整合。 综上所述,以上是实现Spring Security整合Swagger的方法。通过配置Spring Security和静态资源,以及添加相关依赖,我们可以实现在Spring Boot项目中使用Spring Security保护接口并允许Swagger UI的访问[1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值