Easy-Security Easy-Security
首页
  • 快速使用
  • 架构介绍
  • 核心功能
版本说明
GitHub (opens new window)
首页
  • 快速使用
  • 架构介绍
  • 核心功能
版本说明
GitHub (opens new window)
  • 快速入门

    • 简介
    • 快速开始
    • 配置介绍
      • 常用配置
      • 其他配置
      • Yapi规则描述
  • 架构介绍

    • 架构介绍
    • 微服务使用
  • 核心功能

    • 数据加密
    • 自定义扩展
目录

配置介绍

# 常用配置

在快速开始中对用到的配置进行了简单的介绍,那么他们具体做了哪些?

easy
  security:
    # 开启认证
    auth-enable: true
    # 开启鉴权
    authorize-enable: true
    # 开启 Req 请求封装
    request-data-enable: true
    # 项目路径,不会被认证,但依然会封装 Req
    project-url: 
      - /sysUser/login
      - /goods/getIndex
      - /goods/getGoodsInfo
    # 需要加解密的路径,前端数据解密,后端返回数据加密
    decrypt-url: ""
    # 特殊路径,不受认证鉴权影响,不会封装 Req
    special-url: 
      - /oss/**
    # 黑名单
    black-list: 
      - 192.168.148.42
      - 192.168.148.41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  • auth-enable:该配置是管理认证是否启用的,默认 false,为 false 表示所有的请求都不会拦截进行认证判断。
  • authorize-enable:该配种用于管理鉴权是否启用,默认 false,为 false 表示所有的请求不会校验权限。

authorize-enable 和 auth-enable 可以一起使用也可以分开使用,当只需要认证不需要鉴权的时候 auth-enable = true 即可,反而只需要鉴权不需要认证的时候 authorize-enable = true 即可。这样的搭配是为了开发者,在开发过程、测试以及调试提供便利。

  • request-data-enable:启用 Req 的方式用来接收数据,这里提供了一个接收数据的标准,封装有 data、user 和 token,您可以直接在你的请求中获取当前操作用户的数据,也可以获取请求数据以及 token。

request-data-enable 搭配的都是 post 请求,因为都是 json 格式转换

  • project-url:当你的某些路径不需要认证以及授权的时候,但是又开启了认证和授权,就可以在这里配置路径。
  • special-url:当你有一些其他的访问格式,又不想收权限和认证,可以在这里配置路径,比如某些 GET 请求方式等。
  • decrypt-url:是一个解密路径,当你的需求中出现数据需要密文给后端,以防止敏感数据劫持,前端需要把请求数据加密,而后端需要解密,可以把需要解密的路径配置在这里。

decrypt-url 不止会对前端传输的加密数据解密,还会对返回后的结果进行加密,所以前端得到数据后需要自行解密。

  • black-list:发现一些恶意请求的,可以在这里配置他的 IP 拒绝他的访问。

# 其他配置

  • tokenKey:描述前端传给后端 token 的属性名称,默认是 token
  • errorUrl:当请求的路径没有登录或授权等的时候会返回 404,这是因为过滤器会把异常 forward 到一个接口中,接口路径默认为 /failure/authenticationFilter;建议大家一定要配置
@ControllerAdvice
@RestControllerAdvice
@RestController
public class Error {

    private static final Log log = LogFactory.getLog(Error.class);

    /**
     * 处理Exception异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Rep handleAllExceptions(Exception e, WebRequest request) {
        if (e instanceof NoHandlerFoundException) {
            return Rep.error(BasicCode.BASIC_CODE_404);
        }
        if(e instanceof BasicException){
            return Rep.error(((BasicException) e).getCode(),e.getMessage());
        }
        if(e instanceof IllegalArgumentException){
            return Rep.error(BasicCode.BASIC_CODE_99999.getCode(),e.getMessage());
        }
        if(e instanceof MissingServletRequestParameterException){
            log.error(e.getMessage());
            return Rep.error(BasicCode.BASIC_CODE_99998.getCode(),BasicCode.BASIC_CODE_99998.getMsg());
        }
        if(e instanceof HttpRequestMethodNotSupportedException){
            String msg = e.getMessage();
            msg = BasicCode.BASIC_CODE_99997.getMsg() +
                    (StrUtil.isEmpty(msg) ? "": msg.substring(msg.indexOf("'")-1, msg.lastIndexOf("'")+1));
            return Rep.error(BasicCode.BASIC_CODE_99997.getCode(),msg);
        }
        e.printStackTrace();
        String msg = StrUtil.isBlank(e.getMessage()) ? BasicCode.BASIC_CODE_500.getMsg() : e.getMessage();
        return Rep.error(BasicCode.BASIC_CODE_500.getCode(), msg);
    }
    /**
     * easy-security 会把抛出的异常再次请求到该方法中,只需要捕获并包装即可
     */
    @RequestMapping("/failure/authenticationFilter")
    public Rep error(@RequestParam("code") Integer code, @RequestParam("msg") String msg) {
        return Rep.error(code, msg);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# Yapi 规则描述

YAPI 是一个优秀的代码无侵入的接口文档,只需要按照我们正常的注解去描述我们方法的作用,使用 Idea 插件,就会自动帮我们生成接口文档。

本框架对 YAPI 做了一些小功能,隐藏 Req 类中多余的字段,只放出 data 属性显示到文档中,您需要在项目中的 application.yml 文件里面添加如下:

field:
  required: "@com.aizuda.easy.security.annotation.yapi.YApiRule#required"
  default:
    value: "#default"
json:
  rule:
    field:
      ignore: "@com.aizuda.easy.security.annotation.yapi.YApiRule#hide"

method:
  additional:
    header[!@com.aizuda.easy.security.annotation.yapi.YApiRule]: '{name: "token",value: "ddb9ba4f842529e539560b0a6df23408de27dbed00860a1c3c5af04d44fa3c39",required: true}'
1
2
3
4
5
6
7
8
9
10
11
12

如果不使用配置则会在这个文档中显示多余的 user 和 token 字段。

上次更新: 11/23/2023, 1:38:53 PM
快速开始
架构介绍

← 快速开始 架构介绍→

Theme by Vdoing | Copyright © 2023-2023
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式