2022-07-26 hutool工具包部分工具使用(不定时更新)

本文档介绍了如何使用Hutool库进行AES加密和解密操作,以及构建HTTP Post请求。首先展示了如何配置和使用AES工具类进行加解密,接着演示了在控制器中构造HTTP请求向第三方接口发送数据的方法。适用于需要加密敏感信息和调用外部API的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、AES加密解密工具类

二、构造http请求工具


 使用依赖:

            <!--http包-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-http</artifactId>
                <version>5.7.22</version>
            </dependency>
            <!--加密包-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-crypto</artifactId>
                <version>5.8.4</version>
            </dependency>

一、AES加密解密工具类

场景:

        对一个字段进行简单的加密解密时使用,工具类如下

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.shuyun.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * @author hanyang
 * @date 2022-07-13
 */
@Slf4j
@Component
public class AESUtils {

    private static AES aes;

    private static String aesKey;

    @Value("${aes.key}")
    public void setAesKey(String aesKey) {
        AESUtils.aesKey = aesKey;
    }

    public static String getAesKey() {
        return aesKey;
    }

    /**
     * 使用aes加密内容
     *
     * @param content 内容
     * @return 密文
     */
    public static String encryptAes(String content) {
        String aesKey = getAesKey();
        byte[] keyByte = Base64.decode(aesKey);
        // 构建
        aes = SecureUtil.aes(keyByte);
        return aes.encryptHex(content);
    }

    /**
     * 使用aes解密密文
     *
     * @param content 内容
     * @return 明文
     */
    public static String decryptAes(String content) {
        String aesKey = getAesKey();
        byte[] keyByte = Base64.decode(aesKey);
        // 构建
        aes = SecureUtil.aes(keyByte);
        return aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
    }
}

AES属于对称加密,加密解密使用同一个密钥,因此不能每次都生成一个密钥,而是将固定密钥配置在配置文件或使用其他存储方法,生成密钥代码如下。

    /**
     * hutool随机生成一个密钥
     */
    @Test
    public void test1() {
        // 随机生成密钥
        byte[] key =             
        SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        // Base64编码
        String encode = Base64.encode(key);
        System.out.println("key->  " + encode);
    }

运行结果下入:

二、构造http请求工具

场景:

        在某些业务控制器中,我们需要向第三方接口发送调用请求,以获得一些业务需要的信息,比方获取第三方用户信息,需要调用第三方用户的接口。这时就要在控制器中构造http请求。

            String validRes = HttpUtil.createPost(tokenUrl)    
                    //添加请求头
                    .addHeaders(personHeaderToken)
                    //添加表单参数
                    .form(validResMap)
                    //设置contentType
                    .contentType("application/x-www-form-urlencoded")
                    //执行请求并返回响应体
                    .execute().body();

以Post请求为例,可以在构造请求的时候设置一些请求头信息,表单、请求体等多种参数构造方式,注意参数基本都是Map类型

对于获取的结果,可以采用FastJson等json处理工具类进行转换,转换成map后取值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值