package com.ard.vtdu.client; import com.ard.vtdu.config.MediamtxConfig; import com.dtflys.forest.http.ForestRequest; import com.dtflys.forest.interceptor.ForestInterceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Base64; /** * 这个拦截器暂时先不使用,需要时在启用 * 用于mediaMtx流媒体接口安全请求 * * @author lsy * @date 2025/10/16 */ @Component @Slf4j public class MediamtxAuthInterceptor implements ForestInterceptor { /** * 该方法在请求发送之前被调用, 若返回false则不会继续发送请求 * * @Param request Forest请求对象 */ @Override public boolean beforeExecute(ForestRequest req) { try { String username = "admin"; String password = "mypass"; //对应mediamtx.yml //- user: argon2:$argon2id$v=19$m=4096,t=3,p=1$c2FsdEl0V2l0aFNhbHQ$Mi4H+feNHQk/E0sm46PEz3dnHPTzZMNWV5LVdDD/4SI // pass: argon2:$argon2id$v=19$m=4096,t=3,p=1$c2FsdEl0V2l0aFNhbHQ$5VEdlOBG/er50vvc4GZO4uA3juhk/uvUzYOzTMv6RiQ // 拼接用户名和密码 String authString = username + ":" + password; // Base64 编码 String encodedAuth = Base64.getEncoder().encodeToString(authString.getBytes()); // 最终的 Authorization 头 String authorizationHeader = "Basic " + encodedAuth; log.debug("Authorization header: " + authorizationHeader); req.addHeader("Authorization", authorizationHeader); } catch (Exception e) { log.error("TokenInterceptor 处理异常:{}", e.getMessage()); return false; } return true; } }