Skip to content

ruoyi-crypto (加解密组件)

[^使用 sm4 算法对响应体加密和对请求体解密]:

  • maven引入

    xml
    <dependency>
        <groupId>com.mfwl.ruoyi</groupId>
        <artifactId>ruoyi-crypto</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.mfwl.ruoyi</groupId>
                <artifactId>ruoyi-web</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
  • 主要包含三个注解 @EnableCrypto, @NoDecrypt, @NoEncrypt。

    使用前需要先配置加密 key

    yaml
    ruoyi:
      crypto:
        key: xxxxxxx
  • 加密 key 的生成方式为:

    java
    public static void main(String[] args){
      // 原始字符串只能为16个字符
      String originKey = "aaaaaaaaaaaaaaaa"
      // 生成的加密key
      String key =  SM4Util.toHex(originKey)
    }
  • @EnableCrypto 注解为开启加解密功能,使用方式为添加到启动类上。

    java
    @SpringBootApplication
    @EnableCrypto
    public class Application {
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    }
  • @NoDecrypt 为单独关闭某个接口的解密功能,可添加到 controller 的类或者方法上

    java
    @Api(tags = "小程序:用户相关接口")
    @RequestMapping("/frontend/user")
    @RequiredArgsConstructor
    @RestController
    @RWrapper
    @NoDecrypt//作用于类上,此类得所有接口不进行body参数解密
    public class FrontendUserController {
    
    	private final ClientUserService clientUserService;
        
    	@ApiOperation("个人中心用户信息")
    	@GetMapping("/info")
        @NoDecrypt//作用于方法上,此方法接口不进行body参数解密
    	public FrontendLoginUserIdentityVO getUserInfo() {
    		return null;
    	}
    }
  • @NoEncrypt 为单独关闭某个接口的加密功能,可添加到 controller 的类或者方法上

    java
    @Api(tags = "小程序:用户相关接口")
    @RequestMapping("/frontend/user")
    @RequiredArgsConstructor
    @RestController
    @RWrapper
    @NoEncrypt//作用于类上,此类得所有接口返回参数不进行加密
    public class FrontendUserController {
    
    	private final ClientUserService clientUserService;
        
    	@ApiOperation("个人中心用户信息")
    	@GetMapping("/info")
        @NoEncrypt//作用于方法上,此方法接口返回参数不进行加密
    	public FrontendLoginUserIdentityVO getUserInfo() {
    		return null;
    	}
    }

本内容仅限内部使用,技术细节以实际代码为准