【问题标题】:Spring oauth2 InsufficientAuthenticationExceptionSpring oauth2 InsufficientAuthenticationException
【发布时间】:2015-02-03 07:33:35
【问题描述】:

具有以下基于 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/*");
    }

}

当尝试访问其中一个 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."}%

奇怪的是,当我将 servlet 的映射从 /api/* 更改为 / 时,它按预期工作。所以肯定有什么问题,但我一无所知?

【问题讨论】:

    标签: spring spring-mvc spring-security spring-security-oauth2


    【解决方案1】:

    解决此问题的方法之一可能是检查您在security.xml 中的身份验证服务器的模式设置:

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

    如果你的 servlet 回答请求的 /api/* 时没问题,我想你需要检查你的模式,并从身份验证服务器模式中的链接中删除 api:将 pattern="/api/oauth/token" 更改为 pattern="/oauth/token"

    【讨论】:

      【解决方案2】:

      您可以在FrameworkHandlerMapping 中设置前缀,例如通过AuthorizationServerEndpointsConfigurer:

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

      【讨论】:

        猜你喜欢
        • 2016-01-07
        • 2017-01-18
        • 2015-07-15
        • 2021-11-27
        • 2017-05-13
        • 2019-04-29
        • 2019-09-25
        • 2017-10-31
        • 2017-09-11
        相关资源
        最近更新 更多