繁体   English   中英

需要认证才能获得访问令牌(不允许匿名)

[英]Authentication is required to obtain an access token (anonymous not allowed)

我尝试修改现有示例-Tonr2和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) 我检查了这个问题 当然,我的用户是匿名的-我想登录Sparklr2。 另外,我尝试了此bean设置的不同组合,但没有任何好处。 如何解决? 如何使其如我所愿?

该职位将近迟到了两年。

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)");
            }
        }

你要么

  • ClientCredentialsResourceDetails中使用OAuth2RestTemplate ,或者
  • 在使用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();
}

旧帖子...

确实确实会从AccessTokenProviderChain引发异常,但是当Spring安全过滤器调用错误顺序时会发生此异常。 确保您的OpenIdAuthenticationFilter在OAuth2ClientContextFilter之后调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM