Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Spring security OAuth2-检索令牌时选项请求的状态401_Spring Security_Oauth 2.0_Cors_Single Page Application_Restful Authentication - Fatal编程技术网

Spring security OAuth2-检索令牌时选项请求的状态401

Spring security OAuth2-检索令牌时选项请求的状态401,spring-security,oauth-2.0,cors,single-page-application,restful-authentication,Spring Security,Oauth 2.0,Cors,Single Page Application,Restful Authentication,我们的堆栈使用主干作为客户端应用程序,使用Spring引导作为RESTful API 我们正在尝试使用OAuth2进行基本身份验证,用户提供用户名和密码 我们使用Spring安全性进行身份验证,使用jQuery$.ajax方法进行请求。然而,我们得到的响应是飞行前选项请求的401(未授权)状态,在此之前,我们甚至可以发布带有授权秘密的标题。 然而,我们可以张贴或获得任何其他资源没有任何问题。OPTIONS请求的服务器响应为200(确定),然后它会跟进POST请求 那么,为什么来自/oauth/t

我们的堆栈使用主干作为客户端应用程序,使用Spring引导作为RESTful API

我们正在尝试使用OAuth2进行基本身份验证,用户提供用户名和密码

我们使用Spring安全性进行身份验证,使用jQuery$.ajax方法进行请求。然而,我们得到的响应是飞行前选项请求的401(未授权)状态,在此之前,我们甚至可以发布带有授权秘密的标题。 然而,我们可以张贴或获得任何其他资源没有任何问题。OPTIONS请求的服务器响应为200(确定),然后它会跟进POST请求

那么,为什么来自/oauth/token的选项请求响应401状态,即使它不应该这样做呢?它不允许我们对自己进行授权,因为它无法在选项请求中添加授权头

这是我们处理前端请求的方式:

                $.ajax({
                url: "http://localhost:8080/oauth/token",
                type: "POST",
                beforeSend: function(xhr) { 
                    xhr.setRequestHeader("Authorization", "Basic Y2xpZW50YXBwOjEyMzQ1Ng==");
                },
                data: {
                    password: "qwerty",
                    username: "jkowalski",
                    grant_type: "password"
                }
            });
这是我们的OAuth2配置:

@Configuration
public class OAuth2ServerConfiguration {

private static final String RESOURCE_ID = "restservice";

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
        ResourceServerConfigurerAdapter {

    [...]

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

        http
        .authorizeRequests()
            .anyRequest().permitAll();
    }

}

[...]

@Configuration
@EnableAuthorizationServer
protected static class OAuth2AuthorizationConfig extends
        AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // @formatter:off
        clients
            .inMemory()
                .withClient("clientapp")
                    .authorizedGrantTypes("password", "refresh_token")
                    .authorities("USER","ADMIN")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .secret("123456");
        // @formatter:on
    }

[...]
}

我可以从理论上回答你的问题:

那么,为什么来自/oauth/token的选项请求响应401状态,即使它不应该这样做呢?它不允许我们对自己进行授权,因为它无法在选项请求中添加授权头

这是因为在令牌端点处只允许完全认证的请求

在您的资源服务器中,您允许所有请求在不进行身份验证的情况下通过以下方式发生:
http.authorizeRequests().anyRequest().permitAll()

我试图解决上述情况,但我无法让它为我工作

为了完整起见,我在这里还提到,您必须添加一个,以便将正确的头添加到选项飞行前请求中


,因此,如果我的问题得到回答,您也可以利用这些信息解决您的问题。

解决了我的问题,这也应该适用于您!签出我的公司过滤器: