以下是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();
}
}
謝謝!
該警告不是特定於使用oauth的東西。當您使用'appliction/x-www-form-urlencoded'時,這是澤西島(我認爲主要是Tomcat)會發生的事情。它只是告訴你,如果你想在Jersey資源方法中使用表單參數,你應該使用'@ FormParam'(不是其他方法)。對我來說,似乎如果你得到這個警告,那麼oauth配置不正確,因爲oauth端點甚至不應該經過Jersey。它應該只是春季安全。 –
@peeskillet謝謝。但是我假設端點沒有通過Jersey(正如您在配置中看到的那樣),那麼爲什麼它會打印OAuth端點的警告? –
關鍵是它不應該穿過澤西島。 Spring Security是一個servlet過濾器,它在Jersey應用程序的範圍之外。所以如果澤西接受oauth端點的請求,那麼有些問題是錯誤的。由於某些原因,Spring Security沒有處理請求。我不確定那是什麼原因。 –