我在 UI 中收到典型的 Cors 錯(cuò)誤。出于測(cè)試原因,我想完全禁用它。我覺(jué)得奇怪的是,我得到了 200 并且我可以在 Chrome 調(diào)試的響應(yīng)中看到“Hello World”,但我得到:從源“ http://localhost: 8000”訪問(wèn)“http://localhost: 8101/app/services/rest/user ”處的XMLHttpRequest已被 CORS 策略阻止:沒(méi)有“Access-Control-Allow-Origin”標(biāo)頭存在于所請(qǐng)求的資源上。我有這個(gè)豆子 <jaxrs:server id="processServices" address="/"> <jaxrs:serviceBeans> <ref bean="userServiceBean"/> </jaxrs:serviceBeans> </jaxrs:server> <bean id="userServiceBean" class="com.package.services.UserService"/> 以及基本服務(wù):@Service("processUserService")public class UserService { @GET @Path("/user") public String getUser() { return "Hello World" }}我在 stackoverflow 上看到,他們告訴我要使用import java.io.IOException;import javax.ws.rs.container.ContainerRequestContext;import javax.ws.rs.container.ContainerResponseContext;import javax.ws.rs.container.ContainerResponseFilter;import javax.ws.rs.ext.Provider;@Providerpublic class CORSFilter implements ContainerResponseFilter { @Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext cres) throws IOException { cres.getHeaders().add("Access-Control-Allow-Origin", "*"); cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); cres.getHeaders().add("Access-Control-Allow-Credentials", "true"); cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); cres.getHeaders().add("Access-Control-Max-Age", "1209600"); }}但仍然是同樣的錯(cuò)誤。我調(diào)試了一下,這個(gè)方法沒(méi)有被調(diào)用。我在這里缺少什么?
1 回答

慕桂英3389331
TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要在您的服務(wù)器上注冊(cè)該提供程序。
就像是:
<jaxrs:server id="processServices" address="/">
<jaxrs:serviceBeans>
<ref bean="userServiceBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="corsfilterProvider" />
</jaxrs:providers>
</jaxrs:server>
<bean id="corsfilterProvider" class="your.filters.full.name"/>
<bean id="userServiceBean" class="com.package.services.UserService"/>
添加回答
舉報(bào)
0/150
提交
取消