Swagger是什么?能干什么?在这就不展开讲解了。本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限。

  1. 引入依赖
<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>

2.SwaggerConfig配置

package com.entfrm.core.swagger.config;

import com.entfrm.core.base.config.GlobalConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.Arrays;
import java.util.Collections; /**
* @author entfrm
* @date 2020/4/14
* @description swagger 配置
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/dev")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.securitySchemes(Collections.singletonList(securitySchemes()))
.securityContexts(Collections.singletonList(securityContexts()));
} /**
* 认证方式使用密码模式
*/
private SecurityScheme securitySchemes() {
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("/dev/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())))
.forPaths(PathSelectors.any())
.build();
} /**
* 允许认证的scope
*/
private AuthorizationScope[] scopes() {
AuthorizationScope authorizationScope = new AuthorizationScope("test", "接口测试");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return authorizationScopes;
} /**
* 添加摘要信息
*/
private ApiInfo apiInfo() {
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
// 设置标题
.title(GlobalConfig.getName())
// 描述
.description(GlobalConfig.getName() + "接口文档")
// 作者信息
.contact(new Contact("entfrm", "http://47.100.3.58/", "1029861695@qq.com"))
// 版本
.version("版本号:" + GlobalConfig.getVersion())
.build();
}
}

3.在Controller中注解@Api,@ApiOperation

/**
* @author entfrm
* @date 2020-04-01 10:04:11
* @description 文章Controller
*/
@Api("文章管理")
@RestController
@AllArgsConstructor
@RequestMapping("/cms/article")
public class ArticleController { private final CategoryService categoryService;
private final ArticleService articleService; @ApiOperation("文章列表")
@PreAuthorize("@ps.hasPerm('article_view')")
@GetMapping("/list")
@ResponseBody
public R list(Page page, Article article) {
IPage<Article> articlePage = articleService.page(page, getQueryWrapper(article));
return R.ok(articlePage.getRecords(), articlePage.getTotal());
}
}

4.重启看下效果

5.码云地址

源码下载

Springboot Oauth2 集成Swagger2权限验证实战的更多相关文章

  1. SpringBoot与Shiro整合权限管理实战

    SpringBoot与Shiro整合权限管理实战 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] *观看本文章需要有一定SpringBoot整合经验* Shiro框架简介 Apach ...

  2. springboot使用jwt进行权限验证

    springboot使用jwt进行权限验证 依赖准备 首先导入对应的依赖 <dependencies> <dependency> <groupId>org.apac ...

  3. SpringBoot整合Apache Shiro权限验证框架

    比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...

  4. SpringBoot RestFul集成Swagger2

    一.依赖: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  5. SpringBoot中集成Swagger2

    1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  6. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  7. springboot、springsecurity、jwt权限验证

    1.背景 基于前后端分离项目的后端模块: 2.相关技术 springboot全家桶 web模块 security模块:用于权限的验证 mongodb 模块:集成mogodb模块 jwt 用于token ...

  8. Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权

    Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...

  9. SpringBoot整合Shiro权限框架实战

    什么是ACL和RBAC ACL Access Control list:访问控制列表 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩,导致在授予时的复杂性,比较分散,不便于管理 例子:常见的文件系 ...

随机推荐

  1. Vue首屏性能优化组件

    Vue首屏性能优化组件 简单实现一个Vue首屏性能优化组件,现代化浏览器提供了很多新接口,在不考虑IE兼容性的情况下,这些接口可以很大程度上减少编写代码的工作量以及做一些性能优化方面的事情,当然为了考 ...

  2. 动态sql & 抽取可重用sql

    抽取可重用的sql片段 抽取:<sql id="xx"></sql> 使用:<include refid="xx">< ...

  3. 如何解决Redis缓存雪崩、缓存穿透

    缓存雪崩 数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 比如一个雪崩的简单过程: 1.redis集群大面积故障 2.缓存失 ...

  4. 为什么建议使用你 LocalDateTime ,而不是 Date?

    在项目开发过程中经常遇到时间处理,但是你真的用对了吗,理解阿里巴巴开发手册中禁用static修饰SimpleDateFormat吗 通过阅读本篇文章你将了解到: 为什么需要LocalDate.Loca ...

  5. Dao、Controller、Service三层的结构划分

     Java Web基础--Controller+Service +Dao三层的功能划分(摘取自网络)1. Controller/Service/DAO简介:      Controller是管理业务( ...

  6. Java学习(十)

    今天学习的是参数的传入,感觉这个和c++差不多. 传一个参数进去,要看这个参数是地址还是一个值,如果是值的话那无论在方法中如何加减,也只是另一个局部变量的事情了,与该参数无关,在原方法中参数的值保持不 ...

  7. Mybatis动态传入tableName--非预编译(STATEMENT)

    在使用Mybatis过程中,你可以体会到它的强大与灵活之处,由衷的为Mybatis之父点上999个赞!在使用过程中经常会遇到这样一种情况,我查询数据的时候,表名称是动态的从程序中传入的,比如我们通过m ...

  8. Linux——搭建Apache(httpd)服务器

    一.基本概念 Apache(或httpd)是Internet上使用最多的Web服务器技术之一,使用的传输协议是http超文本传输协议(一个基于超文本的协议),用于通过网络连接来发送和接受对象. 有两个 ...

  9. javascript-初级-day08

    return <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  10. [IIS]文件夹权限

    发布完iis,默认的 Application Pool 没有权限访问文件夹. 可以给项目文件夹添加用户权限. 右键 - "属性" - "安全" - " ...