9

我尝试修改现有示例 - 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的不同设置组合,但没有什么好处。如何解决?如何让它按我的意愿工作?

4

2 回答 2

2

这个职位迟到了将近两年。

从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();
}

应用程序属性:

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();
}
于 2018-05-03T19:19:38.583 回答
0

旧帖...

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

于 2018-12-05T14:08:00.447 回答