【问题标题】:Authentication is required to obtain an access token (anonymous not allowed)需要进行身份验证才能获取访问令牌(不允许匿名)
【发布时间】:2016-11-01 11:30:21
【问题描述】:

我尝试修改现有示例 - Tonr2 and Sparklr2。 我还查看了基于 Spring Boot Spring Boot OAuth2 的本教程。我尝试像在 Tonr2 示例中那样构建应用程序,但没有首次登录(在 tonr2 上)。我只需要在 Sparklr2 端进行一个身份验证。我这样做:

@Bean
    public OAuth2ProtectedResourceDetails sparklr() {
        AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
        details.setId("sparklr/tonr");
        details.setClientId("tonr");
        details.setTokenName("oauth_token");
        details.setClientSecret("secret");
        details.setAccessTokenUri(accessTokenUri);
        details.setUserAuthorizationUri(userAuthorizationUri);
        details.setScope(Arrays.asList("openid"));
        details.setGrantType("client_credentials");
        details.setAuthenticationScheme(AuthenticationScheme.none);
        details.setClientAuthenticationScheme(AuthenticationScheme.none);
        return details;
    }

但我有 Authentication is required to obtain an access token (anonymous not allowed) 。我检查了this question。当然,我的用户是匿名的——我想登录 Sparklr2。另外,我尝试了这个bean的不同设置组合,但没有什么好处。如何解决?如何让它按我的意愿工作?

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-oauth2


    【解决方案1】:

    这篇文章迟到了将近两年。

    AccessTokenProviderChain抛出异常

            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    
            if (auth instanceof AnonymousAuthenticationToken) {
                if (!resource.isClientOnly()) {
                    throw new InsufficientAuthenticationException(
                        "Authentication is required to obtain an access token (anonymous not allowed)");
                }
            }
    

    你也可以

    • 在您的OAuth2RestTemplate 中使用ClientCredentialsResourceDetails,或
    • 在使用AuthorizationCodeResourceDetails访问外部资源之前对用户进行身份验证

    事实上,在tonr2 and sparklr2 示例中(我个人觉得这个名字很混乱),要访问sparklr2 上的资源,用户必须首先在tonr2 上进行身份验证。如oauth2/tonr

    @Override
    
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("marissa").password("wombat").roles("USER").and().withUser("sam")
                .password("kangaroo").roles("USER");
    }
    

    如果您的用户是匿名用户,您可能需要检查Single Sign On

    如果您只想快速尝试 Oauth2 集成,请将基本身份验证添加到您的应用程序中:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .anyRequest().authenticated().and().httpBasic();
    }
    

    application.properties:

    spring.security.user.password=password
    spring.security.user.name=user
    

    不要忘记将spring-boot-starter-security 添加到您的项目中。

    例如在gradle中:compile 'org.springframework.boot:spring-boot-starter-security'

    或者您也可以禁用AnonymousAuthenticationToken 创建者:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.anonymous().disable();
    }
    

    【讨论】:

      【解决方案2】:

      旧帖...

      这个异常确实是从 AccessTokenProviderChain 抛出的,但是当 spring 安全过滤器调用错误的顺序时会发生这种情况。确保您的 OpenIdAuthenticationFilter 在 OAuth2ClientContextFilter 之后调用。

      【讨论】:

        猜你喜欢
        • 2017-05-13
        • 2018-06-12
        • 2018-04-30
        • 2020-07-01
        • 2019-10-24
        • 2019-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多