2

다음 web.xml의 클래스 기반 구성을 갖는봄 OAuth2를 InsufficientAuthenticationException

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."}% 

이 이상한를 서블릿의 매핑을/api/*에서 /로 변경하면 예상대로 작동합니다. 그래서 무언가 틀렸어 야하지만 우둔한거야?

답변

4

예 : FrameworkHandlerMapping에 접두어를 설정할 수 있습니다. 이 문제에 대한 해결책의

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

security.xml에 인증 서버의 사용자 패턴 설정을 확인 할 수 있습니다 : 그것은 확실히 경우

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

당신은 요청의에 서블릿 응답을 할 때 AuthorizationServerEndpointsConfigurer 통해 /api/*, 패턴을 확인하고 인증 서버 패턴의 링크에서 api을 삭제해야합니다. pattern="/api/oauth/token"pattern="/oauth/token"

관련 문제