All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shoulder.crypto.negotiation.exception.NegotiationErrorCodeEnum Maven / Gradle / Ivy

Go to download

Shoulder 提供的 协商加密模块,用于非信任网络环境下的安全通信。基于 DH + ECC 实现先进的加密算法协商算法,比传统的 DH + DES 协商算法性能显著更高,更安全。

There is a newer version: 1.0.0-M2.2
Show newest version
package org.shoulder.crypto.negotiation.exception;

import jakarta.annotation.Nonnull;
import org.shoulder.core.exception.ErrorCode;
import org.shoulder.crypto.exception.CipherRuntimeException;
import org.slf4j.event.Level;
import org.springframework.http.HttpStatus;


/**
 * 密钥协商错误码
 * 5500-5600
 * 这里定义的异常必须是可以对外部抛出的,不应把全部加解密异常定义错误码或转为运行时异常,该部分应当尽量减少可对外暴露的细节
 *
 * @author lym
 */
public enum NegotiationErrorCodeEnum implements ErrorCode {

    // ------------------------------- 对称加解密 -----------------------------

    /**
     * 参数错误:安全会话参数缺失(xSessionId、token、xDk)
     */
    MISSING_REQUIRED_PARAM(5501, "negotiation session param missing", Level.WARN, HttpStatus.BAD_REQUEST),

    /**
     * 协商失败
     */
    NEGATION_FAIL(5510, "negotiation fail.", Level.ERROR, HttpStatus.INTERNAL_SERVER_ERROR),

    /**
     * Token无效
     */
    TOKEN_INVALID(5511, "token invalid.", Level.INFO, HttpStatus.FORBIDDEN),

    /**
     * 无效的密钥:未协商,或【已过期】需要重新协商
     */
    NEGOTIATION_INVALID(5512, "negotiation invalid.", Level.INFO, HttpStatus.FORBIDDEN),

    /**
     * 数据加密失败【按理不会出现】:签名失败
     */
    ENCRYPT_FAIL(5513, "negotiation encrypt fail.", Level.ERROR, HttpStatus.INTERNAL_SERVER_ERROR),

    /**
     * 数据解密失败【按理不会出现】:验签失败
     */
    DATA_DECRYPT_FAIL(5514, "negotiation decrypt fail.", Level.WARN, HttpStatus.FORBIDDEN),

    ;

    private String code;

    private String message;

    private Level logLevel;

    private HttpStatus httpStatus;

    NegotiationErrorCodeEnum(long code, String message, Level logLevel) {
        // 默认 403 拒绝访问
        this(code, message, logLevel, HttpStatus.FORBIDDEN);
    }

    NegotiationErrorCodeEnum(long code, String message, Level logLevel, HttpStatus httpStatus) {
        String hex = Long.toHexString(code);
        this.code = "0x" + "0".repeat(Math.max(0, 8 - hex.length())) + hex;
        this.message = message;
        this.logLevel = logLevel;
        this.httpStatus = httpStatus;
    }

    @Nonnull
    @Override
    public String getCode() {
        return code;
    }

    @Nonnull
    @Override
    public String getMessage() {
        return message;
    }

    @Nonnull
    @Override
    public Level getLogLevel() {
        return logLevel;
    }

    @Nonnull
    @Override
    public HttpStatus getHttpStatusCode() {
        return httpStatus;
    }

    // 改为 NegotiationException

    @Override
    public CipherRuntimeException toException(Object... args) {
        return new CipherRuntimeException(this, args);
    }

    @Override
    public CipherRuntimeException toException(Throwable t, Object... args) {
        return new CipherRuntimeException(this, t, args);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy