yeg
2026-09-05 890049eeece6fc94cd974368451cdc36ec89fdf4
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
package com.battery.business.runtime;
 
import java.net.InetAddress;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
/** 将临时 BTS 模拟器页面和控制 API 限制在本机。 */
@Configuration
public class BtsDevelopmentSimulatorWebConfig implements WebMvcConfigurer
{
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(new LocalOnlyInterceptor())
                .addPathPatterns("/dev/bts-simulator.html", "/dev/bts-simulator/**");
    }
 
    public static final class LocalOnlyInterceptor implements HandlerInterceptor
    {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception
        {
            if (isLoopback(request.getRemoteAddr())) return true;
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "BTS simulator controls are local-only");
            return false;
        }
 
        private boolean isLoopback(String remoteAddress)
        {
            try
            {
                return remoteAddress != null && InetAddress.getByName(remoteAddress).isLoopbackAddress();
            }
            catch (Exception ignored)
            {
                return false;
            }
        }
    }
}