Java 使用Spring配置Apache CXF CrossOriginResourceSharingFilter
如何在不更改源代码(带注释的类或beans.xml)的情况下配置ApacheCXF 在中,配置是硬编码的: 下面的测试代码显示了如何在资源和单个方法级别应用CrossoriginResourceShared注释 [……]Java 使用Spring配置Apache CXF CrossOriginResourceSharingFilter,java,spring,cors,jax-rs,cxf,Java,Spring,Cors,Jax Rs,Cxf,如何在不更改源代码(带注释的类或beans.xml)的情况下配置ApacheCXF 在中,配置是硬编码的: 下面的测试代码显示了如何在资源和单个方法级别应用CrossoriginResourceShared注释 [……] @GET @CrossOriginResourceSharing( allowOrigins = { "http://area51.mil:31415" }, allowCredentials = true, exposeHeaders = {
@GET
@CrossOriginResourceSharing(
allowOrigins = { "http://area51.mil:31415" },
allowCredentials = true,
exposeHeaders = { "X-custom-3", "X-custom-4" }
)
@Produces("text/plain")
@Path("/annotatedGet/{echo}")
public String annotatedGet(@PathParam("echo") String echo) {
return echo;
}
我使用beans.xml配置allowOrigins
属性:
<bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
<property name="allowOrigins">
<list>
<value>myserver1</value>
<value>myserver2</value>
</list>
</property>
</bean>
在可重用WAR中配置
CrossOriginResourceSharingFilter
的预期/首选方法是什么?如果使用某个环境变量设置逗号分隔的源列表,如下图所示:
<bean id="cors-filter"
class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
<property name="allowOrigins" value="#{systemProperties['origins'] != null ? systemProperties['origins'].split(',') : null}">
</property>
</bean>
这样,如果您不向Java VM选项添加-Dorigins,将使用空列表
基于,您可以使用所有对象方法:
作为方法调用的一个示例,我们对字符串文本调用“concat”方法。
Expression=parser.parseExpression('Hello World.concat('!')代码>
<bean id="cors-filter"
class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
<property name="allowOrigins" value="#{systemProperties['origins'] != null ? systemProperties['origins'].split(',') : null}">
</property>
</bean>
<bean id="cors-filter"
class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
<property name="prop" value="#{systemProperties['test'] != null ? systemProperties['test'].split(',') : {}}">
</property>
</bean>