Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Spring server中使用OAuth身份验证时CORS失败_Angularjs_Oauth_Spring Boot_Cors_Jhipster - Fatal编程技术网

Angularjs 在Spring server中使用OAuth身份验证时CORS失败

Angularjs 在Spring server中使用OAuth身份验证时CORS失败,angularjs,oauth,spring-boot,cors,jhipster,Angularjs,Oauth,Spring Boot,Cors,Jhipster,当我试图对调用“oauth/token”端点的spring服务器进行身份验证时,我遇到了CORS问题。 服务器以401响应进行应答 XMLHttpRequest cannot load http://localhost:8080/oauth/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on

当我试图对调用“oauth/token”端点的spring服务器进行身份验证时,我遇到了CORS问题。 服务器以401响应进行应答

 XMLHttpRequest cannot load http://localhost:8080/oauth/token.
 Response to preflight request doesn't pass access control check: 
 No 'Access-Control-Allow-Origin' header is present on the requested resource. 
 Origin 'http://localhost:8000' is therefore not allowed access. 
 The response had HTTP status code 401.
这是我如何称呼服务器的:

 login: function(credentials) {
        var data = "username=" +  encodeURIComponent(credentials.username) + "&password="
            + encodeURIComponent(credentials.password) + "&grant_type=password&scope=read%20write&" +
            "client_secret=mySecretOAuthSecret&client_id=awhyapp";
        return $http.post('http://localhost:8080/oauth/token', data, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json",
                "Access-Control-Allow-Origin": "*",
                "Authorization": "Basic " + Base64.encode("awhyapp" + ':' + "mySecretOAuthSecret")
            }
        }).success(function (response) {
            var expiredAt = new Date();
            expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
            response.expires_at = expiredAt.getTime();
            localStorageService.set('token', response);
            return response;
        });
OAuthConfiguration:

 @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
        .and()
            .exceptionHandling()
            .authenticationEntryPoint(authenticationEntryPoint)
        .and()
            .logout()
            .logoutUrl("/api/logout")
            .logoutSuccessHandler(ajaxLogoutSuccessHandler)
        .and()
            .csrf()
            .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
            .disable()
            .headers()
            .frameOptions().disable()
        .and()
            .authorizeRequests()
            .antMatchers("/api/authenticate").permitAll()
            .antMatchers("/api/register").permitAll()
            .antMatchers("...").permitAll()
    }
}

我遵循了这个问题()中的答案,但我的问题是,无论我如何配置我的筛选器类,即使我使用@Order annotation设置了最高优先级,这些函数也永远不会被调用。

访问控制允许源对请求来说是多余的。这是一个响应头

在链接答案中,将零件更改为:

response.setHeader(“访问控制允许头”,“x请求,授权,接受,内容类型”);
此更改列出了将发送到服务器的请求中允许的标头值

我不确定您的spring安全配置,但您基本上应该允许该端点上的
选项
请求在没有身份验证的情况下工作


祝你好运。

应用程序中取消对cors的评论。yml

cors: #By default CORS are not enabled. Uncomment to enable.
        allowed-origins: "*"
        allowed-methods: GET, PUT, POST, DELETE, OPTIONS
        allowed-headers: "*"
        exposed-headers:
        allow-credentials: true
        max-age: 1800

当我遇到同样的错误时,这对我来说是有效的

我知道头在请求中是无用的,但我尝试了不可能的方法使它工作。。。您建议的问题是,当我调用oauth/token端点时,我的自定义筛选器类(实现javax.servlet.Filter)从未被调用,即使我使用@Order注释给予的是最高优先级。这是由于您的spring安全配置:它显然需要对每个调用进行身份验证,它应该允许
选项
请求。我被完全相同的问题困住了。。。你找到解决办法了吗?