2

以下是Spring Boot的示例:example code from GitHub一切似乎都正常。Spring Boot with Jersey and Spring Security OAuth2

但是,当我将Spring Boot Security OAuth2集成到项目中时,我的OAuth2端点停止工作。有一个在日志中警告:

2017-05-04 08:56:24.109 WARN 2827 --- [nio-8080-exec-1] o.glassfish.jersey.servlet.WebComponent : A servlet request to the URI http://127.0.0.1:8080/oauth/token contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

这让我觉得即使我不登记端点,泽西岛被捕获并处理的机身,使得Spring MVC的无法接受的请求......

我的球衣配置为:

@Component 
public class JerseyConfig extends ResourceConfig { 

    public JerseyConfig() { 
     register(InfoController.class); 
    } 

} 

而且我的信息控制器是非常简单的:

@Component 
@Path("/me") 
@Produces("application/json") 
public class InfoController { 
    @GET 
    public String meAction() { 
    return "Hi"; 
    } 
} 

最后,我试图让,它是造成日志中的警告电话:

curl -X POST -u CLIENT_APPLICATION:123456789 http://127.0.0.1:8080/oauth/token -H "Accept: application/json" -d "password=aaa&username=aa&grant_type=password&client_id=CLIENT_APPLICATION"

有两个项目(spring-boot-starter-jersey在这个意义上spring-security-oauth2之间存在已知的不兼容?

删除泽西岛配置使得所有的工作,但我需要在我的控制器上使用它。

我对OAuth2用户的配置是:

@Configuration 
public class OAuth2ServerConfiguration { 
    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 
    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) { 
     resources.resourceId("OAuth2 Server"); 
    } 
    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
      .authorizeRequests() 
      .antMatchers("/oauth/token").permitAll() 
      .antMatchers("/*").authenticated(); 
     // @formatter:on 
    } 
    } 
} 

再就是安全配置本身:提前

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    private final ApiUserDetailsService userDetailsService; 

    @Autowired 
    public WebSecurityConfiguration(ApiUserDetailsService userDetailsService) { 
    this.userDetailsService = userDetailsService; 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
    } 
} 

谢谢!

+0

该警告不是特定于使用oauth的东西。当您使用'appliction/x-www-form-urlencoded'时,这是泽西岛(我认为主要是Tomcat)会发生的事情。它只是告诉你,如果你想在Jersey资源方法中使用表单参数,你应该使用'@ FormParam'(不是其他方法)。对我来说,似乎如果你得到这个警告,那么oauth配置不正确,因为oauth端点甚至不应该经过Jersey。它应该只是春季安全。 –

+0

@peeskillet谢谢。但是我假设端点没有通过Jersey(正如您在配置中看到的那样),那么为什么它会打印OAuth端点的警告? –

+0

关键是它不应该穿过泽西岛。 Spring Security是一个servlet过滤器,它在Jersey应用程序的范围之外。所以如果泽西接受oauth端点的请求,那么有些问题是错误的。由于某些原因,Spring Security没有处理请求。我不确定那是什么原因。 –

回答

1

泽西似乎正在尝试处理OAuth端点,它不应该这样做。原因是Jersey的默认映射是/*,这意味着它将处理所有URL的请求。您可以更改在几个方面:

  1. ResourceConfig子类的顶部添加@ApplicationPath有不同的映射

    @Component 
    @ApplicationPath("/api") 
    public class JerseyConfig extends ResourceConfig {} 
    
  2. 您可以在application.properties文件中添加映射

    spring.jersey.application-path=/api 
    

这将做什么是前缀/api给所有的泽西岛端点,也导致泽西岛不处理所有请求,只有那些以/api开头的请求。

相关问题