简体   繁体   English

Spring oauth2 InsufficientAuthenticationException

[英]Spring oauth2 InsufficientAuthenticationException

Having the following web.xml class based configuration: 具有以下基于web.xml类的配置:

public class WebApp extends AbstractDispatcherServletInitializer {

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(ClassUtils.getPackageName(getClass()));
        return context;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/api/*"};
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
        filter.setServletContext(servletContext);
        filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
        servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/api/*");
    }

}

When trying to access one of the oauth endpoints I'm getting the following result: 尝试访问oauth端点之一时,得到以下结果:

curl -u core:secret "http://localhost:8081/api/oauth/token?client_id=core&grant_type=password&username=user&password=123&response_type=token&scope=admin" 

{"error":"unauthorized","error_description":"There is no client authentication. Try adding an appropriate authentication filter."}%

The strange this is when I change the servlet's mapping from /api/* to / it works as expected. 奇怪的是,当我将servlet的映射从/ api / *更改为/时,它按预期工作。 So something must be wrong but I'm clueless one what ? 所以一定是有问题的,但我一无所知吗?

You can set a prefix in the FrameworkHandlerMapping , eg through the AuthorizationServerEndpointsConfigurer : 您可以在FrameworkHandlerMapping设置前缀,例如通过AuthorizationServerEndpointsConfigurer

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        String prefix = "/api";
        endpoints.prefix(prefix);
    }
}

One of the solutions to this problem might be checking your pattern settings of your Authentication Server in security.xml : 解决此问题的方法之一可能是在security.xml检查Authentication Server的模式设置:

    <http pattern="/oauth/token"
      create-session="stateless"
      authentication-manager-ref="clientAuthenticationManager"
      use-expressions="true"
      xmlns="http://www.springframework.org/schema/security">

If it is alright when you make your servlet answer to request's /api/* , I guess you need to check your pattern, and remove api from your link in Authentication server pattern: change pattern="/api/oauth/token" to pattern="/oauth/token" 如果您在对请求的/api/*进行servlet答复时还可以,我想您需要检查一下模式,然后从身份验证服务器模式中的链接中删除api :将pattern="/api/oauth/token"更改为pattern="/oauth/token"

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

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