Spring oauth2 InsufficientAuthenticationException

谁说我不能喝 提交于 2019-12-11 04:37:42

问题


Having the following web.xml class based configuration:

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:

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. So something must be wrong but I'm clueless one what ?


回答1:


You can set a prefix in the FrameworkHandlerMapping, e.g. through the AuthorizationServerEndpointsConfigurer:

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



回答2:


One of the solutions to this problem might be checking your pattern settings of your Authentication Server in security.xml:

    <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"



来源:https://stackoverflow.com/questions/27310668/spring-oauth2-insufficientauthenticationexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!