You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Web应用URL映射到自定义URL?AngularJS+Spring4+JBoss EAP7场景

没问题,完全可以实现这个需求!我分两种场景给你详细拆解具体的实现步骤,适配你提到的两种客户需求:

一、客户使用专属子域名(如clientname.mydomain.com)的方案

这种场景相对简单,核心是通过子域名识别客户身份,替代原URL中的clientId参数。

1. JBoss EAP 7 配置子域名路由

首先要在JBoss中配置虚拟主机,让不同子域名指向你的应用:

  • 编辑JBoss的standalone.xml(独立模式)或domain.xml(域模式),在undertow子系统的server节点下添加对应子域名的虚拟主机:
<host name="clientname-host" alias="clientname.mydomain.com">
    <location name="/" handler="welcome-content"/>
    <filter-ref name="server-header"/>
    <filter-ref name="x-powered-by-header"/>
    <http-invoker security-realm="ApplicationRealm"/>
</host>
  • 如果你有大量子域名需求,可以用通配符别名,比如alias="*.mydomain.com",这样所有子域名都会匹配这个虚拟主机。
  • 然后修改你的应用的jboss-web.xml(放在WEB-INF下),指定应用部署到这个虚拟主机:
<jboss-web>
    <virtual-host>clientname-host</virtual-host>
</jboss-web>
  • 最后确保DNS解析配置了通配符记录(*.mydomain.com),指向你的JBoss服务器公网IP。

2. Spring 4 从子域名提取客户标识

原来的clientId从URL参数获取,现在改为从子域名解析:

  • 写一个Spring MVC拦截器,从请求的Host头中提取子域名作为clientId:
public class SubdomainClientIdInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String host = request.getHeader("Host");
        // 拆分域名,比如从clientname.mydomain.com提取clientname
        String[] hostParts = host.split("\\.");
        String clientId = hostParts[0];
        // 将clientId存入request属性,供后续Controller使用
        request.setAttribute("clientId", clientId);
        // 也可以存入ThreadLocal,方便全局业务逻辑调用
        return true;
    }
}
  • 在Spring配置类中注册这个拦截器:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SubdomainClientIdInterceptor());
    }
}

这样你的Controller就可以通过request.getAttribute("clientId")获取客户标识,不再依赖URL参数。

3. AngularJS 适配子域名模式

AngularJS这边要确保API请求自动适配当前子域名,避免硬编码:

  • 配置$http的默认Base URL,动态获取当前域名:
angular.module('myApp')
.config(function($httpProvider) {
    // 动态生成API基础路径,适配当前子域名
    var baseApiUrl = '//' + window.location.host + '/api';
    $httpProvider.defaults.baseUrl = baseApiUrl;
});

这样所有API请求都会自动使用当前子域名,无需修改原有请求逻辑。

二、客户使用独立自定义域名(如dummy.theredomain.com)的方案

这种场景需要客户配合配置域名解析,同时你要维护域名与clientId的映射关系。

1. 客户配置域名解析

让客户将他们的自定义域名通过CNAME记录指向你的主域名(如mydomain.com),或者直接用A记录指向你的JBoss服务器IP。

2. JBoss EAP 7 配置自定义域名虚拟主机

和子域名方案类似,在JBoss中添加对应自定义域名的虚拟主机:

<host name="dummy-host" alias="dummy.theredomain.com">
    <location name="/" handler="welcome-content"/>
    <filter-ref name="server-header"/>
    <filter-ref name="x-powered-by-header"/>
    <http-invoker security-realm="ApplicationRealm"/>
</host>

同样在应用的jboss-web.xml中指定这个虚拟主机。

3. Spring 中映射域名到clientId

因为自定义域名无法直接提取clientId,需要维护一个映射表(可以存在数据库或配置文件中):

  • 写一个拦截器,根据请求域名匹配对应的clientId:
public class DomainClientIdInterceptor extends HandlerInterceptorAdapter {
    // 可以从数据库或配置文件加载这个映射表
    private final Map<String, String> domainToClientIdMap = new HashMap<>();
    
    public DomainClientIdInterceptor() {
        // 示例映射,实际建议从配置中心或数据库读取
        domainToClientIdMap.put("dummy.theredomain.com", "12");
        domainToClientIdMap.put("anotherclient.com", "13");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String host = request.getHeader("Host");
        String clientId = domainToClientIdMap.get(host);
        
        if (clientId == null) {
            // 处理未知域名,返回403或重定向到默认页面
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Invalid custom domain");
            return false;
        }
        
        request.setAttribute("clientId", clientId);
        return true;
    }
}
  • 注册这个拦截器到Spring MVC,逻辑和子域名方案一致。

4. AngularJS 适配自定义域名

和子域名方案完全相同,通过动态获取当前域名配置API的Base URL即可。

三、额外注意事项
  • SSL证书:如果使用HTTPS,需要配置多域名SAN证书(支持多个自定义域名)或通配符证书(支持所有子域名),否则浏览器会提示证书错误。
  • Session隔离:要确保不同客户的Session完全隔离,避免数据泄露。可以基于clientId实现Spring Security的多租户隔离,或者使用独立的Session存储。
  • 反向代理优化:如果客户数量较多,建议用Nginx作为前置反向代理,统一处理域名路由,再转发到JBoss。这样JBoss无需维护大量虚拟主机配置,Nginx的配置示例如下:
server {
    listen 80;
    server_name clientname.mydomain.com;
    location / {
        proxy_pass http://your-jboss-ip:8080/your-app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 80;
    server_name dummy.theredomain.com;
    location / {
        proxy_pass http://your-jboss-ip:8080/your-app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
  • CORS问题:如果客户域名和你的主域名不同,但通过虚拟主机或反向代理直接映射到应用,就不需要配置CORS,因为请求属于同一服务器环境。

内容的提问来源于stack exchange,提问作者LeoScott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:36:01