Spring oauth - Principal为null

时间:2017-07-24 08:04:41

标签: spring oauth spring-security-oauth2

我正在使用spring oauth安全来保护我的休息服务。我有一个授权服务器,因为我在使用正确的凭据呼叫http://localhost:90001/oauth/token时获得了access_token。

我在控制台中执行以下命令:

curl -X POST http://localhost:9001/oauth/check_token?token=55cd2a66-f54a-47f7-b59c-3cea6e2216f5

我收到一条显示已登录用户的json响应。这样才行。当我尝试访问rest端点时,principal为null。这是我的访问点:

@RequestMapping("/user")
// @PreAuthorize("isAuthenticated()")
public Principal user(Principal principal) {
    return principal;
}

我有一个WebSecurityConfigurerAdapter和一个ResourceServerConfigurerAdapter。

我的WebSecurityConfigurerAdapter看起来像这样:

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

  public WebSecurityConfiguration() {
      super(false);
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
  }

  @Override
  protected AuthenticationManager authenticationManager() throws Exception {
    return new ProviderManager(Arrays.asList(new CustomAuthenticationProvider()));
  }
}

我的ResourceServerConfigurerAdapter如下所示:

public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

  public static final String RESOURCE_ID = "MY_RESOURCE_ID";

  @Override
  public void configure(ResourceServerSecurityConfigurer resources) {
    resources.resourceId(RESOURCE_ID);
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
      .anonymous().and()
      .authorizeRequests()
      .antMatchers("/user").authenticated()
      .and().exceptionHandling().accessDeniedHandler(
         new OAuth2AccessDeniedHandler());
  }

}

WebSecurityConfigurerAdapter和ResourceServerConfigurerAdapter之间有什么区别,我的配置中是否存在导致Principal为空的任何内容。

提前致谢, 马亭

0 个答案:

没有答案
相关问题