简体   繁体   English

Spring / OAuth2错误 - InsufficientAuthenticationException,没有客户端身份验证。尝试添加适当的身份验证筛选器

[英]Spring/OAuth2 Error - InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter

I've been stuck for hours trying to figure out what in the world is going wrong with this Spring Security OAuth2 implementation. 我已经被困了几个小时试图弄清楚这个Spring Security OAuth2实现在世界上出了什么问题。

The error occurs when I go to hit the /oauth/token endpoint: 当我去命中/oauth/token端点时发生错误:

localhost:8080/my-oauth-practice-app/oauth/token 本地主机:8080 /我-OAuth的实践应用/的OAuth /令牌

Error : InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter. 错误InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter. InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter.

AUTHORIZATION SERVER CONFIGURATION 授权服务器配置

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


    @Autowired
    @Qualifier("authenticationManagerBean")
    AuthenticationManager authenticationManager;

    @Autowired
    DefaultTokenServices tokenServices;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        super.configure(endpoints);
        endpoints.tokenServices(this.tokenServices).authenticationManager(this.authenticationManager);

    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
        security.tokenKeyAccess("permitAll()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("clientid").secret("clientpass").authorizedGrantTypes("password").scopes("read").autoApprove(true);
    }


}

RESOURCE SERVER CONFIGURATION 资源服务器配置

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    DefaultTokenServices tokenServices;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        super.configure(resources);
        resources.tokenServices(this.tokenServices);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests().anyRequest().hasRole("USER");
    }

}

GENERAL WEB SECURITY CONFIGURATION 一般网络安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Primary
    @Bean
    DefaultTokenServices tokenServices() {
        DefaultTokenServices d = new DefaultTokenServices();
        d.setAccessTokenValiditySeconds(600);
        d.setRefreshTokenValiditySeconds(1000);
        d.setTokenStore(new InMemoryTokenStore());
        return d;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").hasRole("USER");
    }


    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}

You should check WebSecurityConfigurerAdapter method like this: 您应该检查WebSecurityConfigurerAdapter方法,如下所示:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/webjars/**", "/oauth/**");
}

remove "/oauth/**" path. 删除“/ oauth / **”路径。 otherwise 除此以外

TokenEndpoint.postAccessToken(Principal principal, @RequestParam Map<String, String> parameters)

principal will be null. principal将为null。

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

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