最近在学习springsecurity 安全框架,具体是什么概念在这里不一一赘述了。下面呢,咱们一起搭建一下简单的springsecurity swagger 项目感受一下。

  • 首先初始化spring initializer 项目。在项目中引入springsecurity starter。
  • 编写测试类
  • 启动项目。访问项目地址 localhost:8080/test
  • 接着浏览器中出现了一个登录页面 如下



    ps:这个登录页面默认是springsecurity自带的登录页面。 默认的登录账号是 ** user** 密码 是控制台 打印的 一串字符串 例子:Using generated security password: 5334856d-1259-42e2-947b-435bdaa9faf1 每次重启项目都会刷新这个密码。
  • 登录后才能访问接口。

然后在来说一下在springsecurity 中如何配置swagger呢。

首先还是先引入 swagger的 pom配置。 如下

`

<io.springfox.version>2.9.2</io.springfox.version>

    <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${io.springfox.version}</version>
<exclusions>
<exclusion>
<groupId>org.mapstruct</groupId> // 当然 这个东西可以去掉
<artifactId>mapstruct</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>

`

  • 引入后开始编写Swagger2Config 代码如下

`

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Contact;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration

@EnableSwagger2

public class Swagger2Config {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

//为当前包下的controller生成api文档

.apis(RequestHandlerSelectors.basePackage("com.cloud.service.xxx.controller"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {
//设置文档信息
return new ApiInfoBuilder()
.title("XXX接口文档")
.description("XXX接口文档")
.contact(new Contact("xxxx", "http:localhost:8080/doc.html",
"xxxx@xxxx.com"))
.version("1.0")
.build();
}

}

`

  • 特别说明

    如果你的spring 的版本是最新版本,建议你把版本改成2.4.2以下或者是2.4.2 等等版本。启动类不要加什么 @EnableWebMvc 等等注解。 也不要升级guava,它是和springfox 版本同步的。

    当然 你还要配置SpringSecurityConfig extends WebSecurityConfigurerAdapter

    重写 WebSecurity 方法

`

@Override

public void configure(WebSecurity web) throws Exception {

//放行静态资源

web.ignoring().antMatchers(

"/login",

"/logout",

"/css/",

"/js/
",

"/index.html",

"/favicon.ico",

"/doc.html",

"/swagger-ui.html",

"/webjars/",

"/swagger-resources/
",

"/v2/api-docs/**");

}

`

springsecurity 配置swagger的更多相关文章

  1. .net core在网关中统一配置Swagger

    最近在做微服务的时候,由于我们是采用前后端分离来开发的,提供给前端的直接是Swagger,如果Swagger分布在各个API中,前端查看Swagger的时候非常不便,因此,我们试着将Swagger集中 ...

  2. .net core在Ocelot网关中统一配置Swagger

    最近在做微服务的时候,由于我们是采用前后端分离来开发的,提供给前端的直接是Swagger,如果Swagger分布在各个API中,前端查看Swagger的时候非常不便,因此,我们试着将Swagger集中 ...

  3. asp.net core Api配置swagger

    这个很简单的一篇文章用来记录以下使用swagger的过程,以后有用. 1.nuget 下载install-package Swashbuckle.AspNetCore 2.startup里面confi ...

  4. .net core 配置swagger遇到的坑

    Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. Swagger 可以生成 ...

  5. SpringSecurity02 表单登录、SpringSecurity配置类

    1 功能需求 springSecuriy默认的登录窗口是一个弹出窗口,而且会默认对所有的请求都进行拦截:要求更改登录页面(使用表单登录).排除掉一些请求的拦截 2 编写一个springSecurity ...

  6. Swagger学习(二、配置swagger)

    基于上一篇 其实只是在SwaggerConfig.class中配置docket,apiInfo @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 ...

  7. SpringSecurity配置,简单梳理

    生活加油:摘一句子: “我希望自己能写这样的诗.我希望自己也是一颗星星.如果我会发光,就不必害怕黑暗.如果我自己是那么美好,那么一切恐惧就可以烟消云散.于是我开始存下了一点希望—如果我能做到,那么我就 ...

  8. MVC-WebApi配置 Swagger(Web Api可视化文档)

    一.从创建MVC WebApi开始 第一步创建MVC WebApi就创建好了,接下来就进入正题,上干货 ================================================ ...

  9. 【Docker】 .Net Core 3.1 webapi 集成EF Code First,使用MySql进行业务操作 、配置swagger (三)

    系列目录: [Docker] CentOS7 安装 Docker 及其使用方法 ( 一 ) [Docker] 使用Docker 在阿里云 Centos7 部署 MySQL 和 Redis (二) [D ...

  10. SpringMVC 中配置 Swagger 插件.

    一.简介 Swagger的目标是为REST API定义一个与语言无关的标准接口,允许用户发现和理解计算机服务的功能,而无需访问源代码.当通过Swagger正确定义时,用户可以用最少量的实现逻辑理解远程 ...

随机推荐

  1. T137223 节能主义

    设平均数为$x$,那么有差值数组$b_i=a_i-x$. 考虑用类似于均分纸牌的方法来解决本题,从左到右依次考虑每堆书,直接乘上预处理好的组合数,然后清零$b_i$. 在实际操作中,将冗余的操作忽略, ...

  2. java8线程池创建并使用

    1.创建@Configurationpublic class ThreadPoolConfig { /** * 创建线程池 */ @Bean(name = "threadPool" ...

  3. vmhost永久免费主机搭建wordpress

    vmhost主机试用+worpress搭建 点击vmhost进入vmhost官网,vmhost提供了永久免费的主机,还附带一个三级域名,并且支自定义子域名,免费托管5G的网页空间,网页支持php语言, ...

  4. NGINX一次电脑自己可以访问其它IP访问不了

    配制好NGINX 本地电脑curl http..... 正常访问...其它电脑不可以 第一想法防火墙 查一下  firewall-cmd --state not running 然后查下是不是服务开启 ...

  5. IaaS--云上虚拟网络(何恺铎《深入浅出云计算》笔记整理)

    [概念] 虚拟私有网络(Virtual Private Cloud,简称 VPC),是云计算网络端最重要的概念之一,它是指构建在云上的.相互隔离的.用户可以自主控制的私有网络环境.虚拟私有网络有时也称 ...

  6. 基2和基4FFT

    1.1 FFT的必要索引变换 基2算法需要位顺序的反转位逆序,而基4算法需要首先构成一个2位的数字,再反转这些数字,称为数字逆序. 1.1 位逆序和数字逆序 1.2 FFT的复数乘法转实数乘法 \[X ...

  7. EMQX Cloud Serverless 正式上线:三秒部署、按量计费的 MQTT Serverless 云服务

    近日,全球领先的开源物联网数据基础设施软件供应商 EMQ 正式发布了 MQTT Serverless 云服务 -- EMQX Cloud Serverless 的 Beta 版本,开创性地采用弹性多租 ...

  8. 学习-Vue2-Vue实例-数据与方法-数据的响应式

    当一个实例被创建时,它将data对象中的所有的property加入到Vue的响应式系统中. 当这些property的值发生改变时,视图将会产生"响应",即匹配更新为新的值.当这些数 ...

  9. Codeforces 197A Plate Game

    一.题意 你有一个长方形的桌子,长度 a ,宽度 b ,以及无限多的半径 r的圆盘. 两位玩家玩以下游戏:他们轮流把圆盘放在桌子上,使得盘子之间不能互相重叠(但他们的边缘可以互相接触),任何盘子上的任 ...

  10. 微信小程序-关闭某个页面分享

    方式一: wx.hideShareMenu();    方式二: