从零搭建 Spring Boot 后端项目(五)

野性酷女 2023-02-28 05:44 172阅读 0赞

简介

这一小节主要是,整合Swagger2

步骤

  • 在pom.xml下添加如下依赖

    1. <!-- swagger2 -->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. <version>2.7.0</version>
    6. </dependency>
    7. <!-- swagger-ui -->
    8. <dependency>
    9. <groupId>io.springfox</groupId>
    10. <artifactId>springfox-swagger-ui</artifactId>
    11. <version>2.7.0</version>
    12. </dependency>
  • com.example.backend_template.config下新建Swagger2Config类

    1. package com.example.backend_template.config;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import springfox.documentation.builders.ApiInfoBuilder;
    6. import springfox.documentation.builders.PathSelectors;
    7. import springfox.documentation.builders.RequestHandlerSelectors;
    8. import springfox.documentation.service.ApiInfo;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spring.web.plugins.Docket;
    11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    12. /**
    13. * @ClassName Swagger2Config swagger的配置内容即是创建一个Docket实例
    14. * @Description
    15. * @Author L
    16. * @Date Create by 2020/6/30
    17. */
    18. @Configuration
    19. @EnableSwagger2 //启用swagger2
    20. public class Swagger2Config {
    21. //是否开启 swagger-ui 功能,默认为false
    22. @Value("${swagger.enable:false}")
    23. private Boolean enable;
    24. @Bean
    25. public Docket createRestApi() {
    26. return new Docket(DocumentationType.SWAGGER_2)
    27. .enable(enable)
    28. .pathMapping("/")
    29. .apiInfo(apiInfo())
    30. .select()
    31. //需要Swagger描述的接口包路径,如果不想某接口暴露,可在接口上加@ApiIgnore注解
    32. .apis(RequestHandlerSelectors.basePackage("com.example.backend_template.controller"))
    33. .paths(PathSelectors.any())
    34. .build();
    35. }
    36. //配置在线文档的基本信息
    37. private ApiInfo apiInfo() {
    38. return new ApiInfoBuilder()
    39. .title("BackendTemplate项目")
    40. .description("使基于SpringBoot的后端开发变得简单")
    41. .version("1.0")
    42. .build();
    43. }
    44. }
  • application-dev.properties中添加如下配置

    1. # Swagger
    2. # 是否启用Swagger
    3. swagger.enable=true
  • 使用 swagger 注解

    • @Api注解可以用来描述当前Controller的功能
    • @ApiOperation注解用来描述Controller的方法的作用
    • @ApiResponses 用在Controller的方法上,说明Response集
    • @ApiResponse 用在@ApiResponses里边
    • @ApiImplicitParam注解用来描述一个参数,可以配置参数的中文含义,也可以给参数设置默认值,这样在接口测试的时候可以避免手动输入
    • 如果有多个参数,则需要使用多个@ApiImplicitParam注解来描述,多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中
    • @ApiModel 描述实体对象或响应对象的意义,用在pojo类上
    • @ApiModelProperty 用在实体对象或响应对象的字段上,描述其属性

    使用以上注解,并简单修改下原RedisController类,修改后的内容如下:

    1. package com.example.backend_template.controller;
    2. import com.example.backend_template.service.RedisService;
    3. import io.swagger.annotations.Api;
    4. import io.swagger.annotations.ApiImplicitParam;
    5. import io.swagger.annotations.ApiOperation;
    6. import org.springframework.web.bind.annotation.*;
    7. import javax.annotation.Resource;
    8. /**
    9. * @ClassName RedisController
    10. * @Description
    11. * @Author L
    12. * @Date Create by 2020/6/26
    13. */
    14. @Api(tags = "redis简单测试接口")
    15. @RestController
    16. @RequestMapping("redis")
    17. public class RedisController {
    18. @Resource
    19. private RedisService redisService;
    20. @ApiOperation("向redis中存储`name`值")
    21. @ApiImplicitParam(name = "name",value = "名称值",defaultValue = "L",required = true)
    22. @PostMapping("/setRedis")
    23. public Boolean setRedis(@RequestBody String name) {
    24. return redisService.set("name", name);
    25. }
    26. @ApiOperation("向redis中取`name`值")
    27. @GetMapping("/getRedis")
    28. public String getRedis() {
    29. return redisService.get("name");
    30. }
    31. }
  • 此时我们因为在项目中已整合了 Spring Security ,那么如果不做额外配置,Swagger2文档路径就会被拦截,此时只需要在Spring Security的配置类SecurityConfig.java中重写父类configure(WebSecurity web)方法,就不会被Spring Security拦截了,修改security/SecurityConfig类为如下代码:

    package com.example.backend_template.security;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.builders.WebSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import org.springframework.util.DigestUtils;

    /**

    • @ClassName SecurityConfig
    • @Description
    • @Author L
    • @Date Create by 2020/6/30
      */
      @Configuration
      @EnableWebSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Autowired
      private UserDetailsServiceImpl userService;
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

      1. //校验用户
      2. auth.userDetailsService(userService).passwordEncoder(new PasswordEncoder() {
      3. //对密码进行加密
      4. @Override
      5. public String encode(CharSequence charSequence) {
      6. return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
      7. }
      8. //对密码进行判断匹配
      9. @Override
      10. public boolean matches(CharSequence charSequence, String s) {
      11. String encode = DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
      12. boolean res = s.equals(encode);
      13. return res;
      14. }
      15. });

      }

      @Override
      protected void configure(HttpSecurity http) throws Exception {

      1. http.authorizeRequests()
      2. .antMatchers("/", "/index", "/login", "/login-error", "/401").permitAll()
      3. .anyRequest().authenticated()
      4. .and()
      5. .formLogin().loginPage("/login").failureUrl("/login-error")
      6. .and()
      7. .exceptionHandling().accessDeniedPage("/401");
      8. http.logout().logoutSuccessUrl("/");

      }

      //使Spring Security不拦截swagger相关文档,以下路径不会经过过滤链
      @Override
      public void configure(WebSecurity web) throws Exception {

      1. //放行swagger
      2. web.ignoring().antMatchers(
      3. "/v2/api-docs",
      4. "/swagger-resources",
      5. "/swagger-resources/**",
      6. "/configuration/ui",
      7. "/configuration/security",
      8. "/swagger-ui.html/**",
      9. "/webjars/**");
      10. //如启用以下配置,则放行所有路径,方便开发使用,但权限管理时需注释
      11. //web.ignoring().antMatchers("/**");

      }
      }

测试

  • 启动backen_template项目,并访问 http://localhost:8080/swagger-ui.html ,出现以下内容即为swagger的主页面
    在这里插入图片描述
  • swagger 还支持接口测试,接下来简单操作下,点击/redis/getRedis并点击Try it out!按钮
    在这里插入图片描述
    但我们并没有得到值,只得到了如下网页
    在这里插入图片描述
    为什么呢,因为 Spring Security 把/redis/getRedis也拦截了,故我们整合Spring Security后,方便开发可暂时在SecurityConfig类中的configure(WebSecurity web)方法中添加如下代码,但权限开发时一定要记得注释,要不然会影响权限开发

    1. //如启用以下配置,则放行所有路径,方便开发使用,但权限管理时需注释
    2. web.ignoring().antMatchers("/**");
  • 添加好后,重新启动,再点击/redis/getRedis并点击Try it out!按钮,即可获得该值(当然,你要先存,才能取)
    在这里插入图片描述
  • 接下来试一下/redis/setRedis
    在这里插入图片描述
  • 结果成功如下
    在这里插入图片描述

项目地址

项目介绍:从零搭建 Spring Boot 后端项目
代码地址:https://github.com/xiaoxiamo/backend-template

下一篇

六、统一请求结果格式

发表评论

表情:
评论列表 (有 0 条评论,172人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot 项目

    简介 一个基于 Spring Boot 的后端开发模板,主要用于减少平时重复的工作量,以及使开发有良好的开发规范,主要功能包括但不限于权限管理、在线接口文档、日志记录、单