nishiwei
6 days ago 3f43e40ed1a1e9f66b3ad2862da9dbc4b710c712
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
45
46
47
48
49
50
51
52
53
54
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;
    }
}