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

标签 spring-mvc authentication spring-security oauth-2.0 spring-security-oauth2

我已经被困了几个小时试图弄清楚这个 Spring Security OAuth2 实现到底出了什么问题。

当我点击 /oauth/token 时发生错误端点:

本地主机:8080/my-oauth-practice-app/oauth/token

错误 :InsufficientAuthenticationException, There is no client authentication. Try adding an appropriate authentication filter.
授权服务器配置

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


}

资源服务器配置
@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");
    }

}

一般网络安全配置
@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();
    }

}

最佳答案

您应该像这样检查 WebSecurityConfigurerAdapter 方法:

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

删除“/oauth/**”路径。除此以外
TokenEndpoint.postAccessToken(Principal principal, @RequestParam Map<String, String> parameters)

主体将为空。

关于spring-mvc - Spring/OAuth2 错误 - InsufficientAuthenticationException,没有客户端身份验证。尝试添加适当的身份验证过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272529/

相关文章:

security - 客户端身份验证对于 k8s 来说足够好吗?

ASP.Net - 在没有 Windows 用户的情况下使用基本身份验证

grails - 使用@Secured闭包时如何获取 Controller 方法参数?

jsf - Spring Security 登录后,我被重定向到 CSS/JS 资源而不是 HTML 页面

grails - 配置Spring Boot Security在Grails 3.0中使用BCrypt密码编码

java - SpringMVC配置

java - 停止 Spring MVC 注解处理

Swift如何使用密码访问钥匙串(keychain)

java - 使用 Android 将图像读取为字节数组:skImageDecoder::Factory 返回 null

java - Fortify 扫描问题 (XSS) 跨站点脚本反射 - 方法将未经验证的数据发送到网络浏览器