0

我正在尝试使用 Spring boot + Oauth 2.0 创建 RestAPI,并希望从另一个 Web 应用程序访问它。

我的 Spring 应用程序部署在 localhost:8585/oauth/token。如果我使用 chrome REST 扩展来调用它,它工作得非常好,但是当我尝试使用 jquery ajax 调用从我的 Web 应用程序中获取 oauth 令牌时,它给了我以下错误

'XMLHttpRequest 无法加载 localhost:8585/oauth/token。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'localhost:8080' 不允许访问。响应具有 HTTP 状态代码 401。

我参考了一些文档,@EnableZuulProxy 可以为服务器启用代理,但它不起作用

应用程序类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Web 应用程序页面使用 Jquery ajax 调用使用 clientid、客户端密码和授权类型作为 client_credential 调用 oauth/token

AND 应用程序.yml

zuul:
routes:
resource: 
path: oauth/*
url: http://localhost:8080/
stripPrefix: false

和 oauth 服务器 http 配置为

.antMatchers("/oauth/token").authenticated() .and().csrf().csrfTokenRepository(csrfTokenRepository()).and() .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);

4

0 回答 0