好的,所以我想发布一个适用于我的本地开发的解决方案,并允许我使用所需的方法......就像 Oak 所说的那样(抱歉,我不确定如何链接他的用户名)并有两个单独的构建/项目。但是,我的前端项目使用 grunt 在特定端口上提供我的代码,并使用一些中间件将端口上的请求定向到前端代码或运行 spring-boot 的服务器。这使我可以像代码真的在同一个项目上运行一样行事,并避免在不同的域/服务器上运行它们时出现任何 CORS 和其他问题。这是我的 grunt 构建中允许我这样做的代码部分:
livereload: {
options: {
debug: true,
middleware: function (connect, options) {
var middlewares = [];
middlewares.push(rewriteModule.getMiddleware([
//Load App under context-root of 'myappcontext/secured'
{from: '^/napiweb/(.*)$', to: '/$1'},
//Redirect slash to myappcontext/secured as convenience
{from: '^/$', to: '/napiweb', redirect: 'permanent'}
//Send a 404 for anything else
//{from: '^/.+$', to: '/404'}
]));
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
options.base.forEach(function () {
// Serve static files.
middlewares.push(connect.static('.tmp'),
connect().use(
'/bower_components',
connect.static('./bower_components')
),
connect.static(appConfig.app));
});
// Make directory browse-able.
//middlewares.push(connect.directory(directory));
return middlewares;
}
}
},
然后我将我的 spring-boot 配置为具有本地开发代理,该代理将特定请求转发到前端。它设置如下:在我的 config.xml 文件中
<config proxy-port="{{http-port}}" console-port="1776">
<console-recording sso="true" rest="true" max-entries="100" enable-debug- logging='true'/>
<sso-cookie name="wamulator" domain=".somedomain.com" session-timeout-seconds="1800"/>
<port-access local-traffic-only="false"/>
<sso-traffic strip-empty-headers="true">
<by-site host="localhost.somedomain.com" port="{{http-port}}">
<cctx-mapping thost="127.0.0.1" tport="8081">
<policy-source>xml={{policy-src-xml}}</policy-source>
</cctx-mapping>
<cctx-mapping thost="127.0.0.1" tport="9000">
<policy-source>xml={{static-src-xml}}</policy-source>
</cctx-mapping>
<cctx-mapping thost="127.0.0.1" tport="8180">
<policy-source>xml={{napi-src-xml}}</policy-source>
</cctx-mapping>
</by-site>
</sso-traffic>
<user-source type='xml'>xml={{usr-src-xml}}</user-source>
<proxy-timeout inboundMillis="0" outboundMillis="0" />
</config>
如您所见,cctx 映射会将一些请求定向到在端口 9000 上提供服务的前端,并将一些请求定向到提供 API 的后端。这是基于 policy-config.xml 和 static-config.xml文件。它们几乎完全相同,唯一的区别在于 authHost 和 cctx 设置,这里以一个为例:
<deployment at='2013-01-31_16:25:12.205-0700'>
<environment id='dev' host='dev.somedomain.com (exposee)'/>
<application id='napi-rest' authHost='localhost.somedomain.com/napiweb/api' cctx='/napiweb/api'>
<authentication scheme='anonymous' name='Anonymous Authentication'> </authentication>
<authorization failure-redirect-url='/denied.html'>
<default format='exposee' value='Allow Authenticated Users'>
<headers>
<success>
...profile-att specific for my organization
</success>
<failure>
<redirect value='/denied.html'/>
</failure>
</headers>
</default>
<rule name='Allow Authenticated Users' enabled='true' allow-takes-precedence='false'>
<allow>
<condition type='role' value='Anyone'/>
</allow>
</rule>
</authorization>
唯一的区别是另一个文件有authHost='localhost.somedomain.com/napiweb/' cctx='/napiweb/' 这会导致调用 API 和调用前端,就好像它们是从同一个项目中提供的一样。然后,当我们将项目推送到我们的存储库时,我们有两个构建周期。一个人使用前端并使用grunt build 创建静态资产,然后将这些文件复制到其余服务器,以便它可以提供它们。这允许我们有单独的项目进行开发,但只有一个服务器为我们的站点提供服务。不理想......因为最终我认为我们应该为前端和后端设置单独的服务器/实例,但由于我们不允许这样做,这使我们能够像在开发过程中那样行事。我希望这可以帮助别人。