springboot+spring security+swagger 配置

一、配置swagger2.x

  1. 添加pom依赖:
        <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>
  1. 启动类或者配置类上添加注解@EnableSwagger2
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sapphire"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("sapphire-spring-boot-demo")
                .description("Sapphire demo project for Spring Boot")
                .version("v1")
                .build();
    }
}
  1. spring security 配置授权认证
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                //swagger权限配置
                .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/**/api-docs").anonymous()
                .anyRequest()
                .authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
  1. 访问地址:

http://localhost:8080/swagger-ui.html

二、配置swagger3.x

注意:

swagger2.x 升级为swagger3.x,有以下几点变化:

  1. 注解由@EnableSwagger2更改为@EnableOpenApi
  2. 文档类型由DocumentationType.SWAGGER_2更改为DocumentationType.OAS_30
  3. 访问地址由http://localhost:8080/swagger-ui.html更改为http://localhost:8080/swagger-ui/index.html
  4. 资源路径的变更
  5. pom依赖由springfox-swagger2springfox-swagger-ui简化为springfox-boot-starter。当然,旧的依赖直接升级版本号也可以
具体配置
  1. 添加pom依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
  1. 启动类或者配置类上添加注解@EnableOpenApi
@EnableOpenApi
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sapphire"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("sapphire-spring-boot-demo")
                .description("Sapphire demo project for Spring Boot")
                .version("v1")
                .build();
    }
}
  1. spring security 配置授权认证
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/swagger-resources/**", "/swagger-ui/**", "/**/api-docs").anonymous()
                .anyRequest()
                .authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
  1. 访问地址:

http://localhost:8080/swagger-ui/index.html

Spring Boot是一个用于创建独立的、基于生产级别的Spring应用程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一套强大的开发工具和约定,使开发人员能够更专注于业务逻辑的实现。 MyBatis Plus是MyBatis的增强工具,它提供了一系列的便利功能和增强特性,使得使用MyBatis更加简单和高效。它包括了代码生成器、分页插件、逻辑删除、乐观锁等功能,可以大大提高开发效率。 Redis是一个开源的内存数据库,它支持多种数据结构,如字符串、哈希、列表、集合、有序集合等。Redis具有高性能、高可用性和可扩展性的特点,常用于缓存、消息队列、分布式锁等场景。 Driver是指数据库驱动程序,它是用于连接数据库和执行SQL语句的软件组件。在Spring Boot中,我们可以通过配置数据源和引入相应的数据库驱动程序来实现与数据库的交互。 Knife4j是一款基于Swagger的API文档生成工具,它提供了更加美观和易用的界面,可以方便地查看和测试API接口。 Swagger是一套用于设计、构建、文档化和使用RESTful风格的Web服务的工具。它可以自动生成API文档,并提供了交互式的界面,方便开发人员进行接口测试和调试。 JWT(JSON Web Token)是一种用于身份验证和授权的开放标准。它通过在用户和服务器之间传递加密的JSON对象来实现身份验证和授权功能,避免了传统的基于Session的身份验证方式带来的一些问题。 Spring SecuritySpring提供的一个安全框架,它可以集成到Spring Boot应用程序中,提供身份验证、授权、攻击防护等安全功能。通过配置Spring Security,我们可以实现对API接口的访问控制和权限管理。 关于Spring Boot + MyBatis Plus + Redis + Driver + Knife4j + Swagger + JWT + Spring Security的Demo,你可以参考以下步骤: 1. 创建一个Spring Boot项目,并引入相应的依赖,包括Spring Boot、MyBatis Plus、Redis、数据库驱动程序等。 2. 配置数据源和数据库驱动程序,以及MyBatis Plus的相关配置,如Mapper扫描路径、分页插件等。 3. 集成Redis,配置Redis连接信息,并使用RedisTemplate或者Jedis等工具类进行操作。 4. 集成Knife4j和Swagger配置Swagger相关信息,并编写API接口文档。 5. 集成JWT和Spring Security配置安全相关的信息,如登录认证、权限管理等。 6. 编写Controller层的代码,实现具体的业务逻辑。 7. 运行项目,通过Swagger界面进行接口测试。 希望以上内容对你有帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值