2015-10-19 18 views
5

Próbuję skonfigurować osobne serwery uwierzytelniania i zasoby dla oauth2. Mogę pomyślnie skonfigurować serwer authrization i mogę uwierzytelniać i generować tokeny dostępu. Teraz chcę skonfigurować serwer zasobów, który może komunikować się z serwerem auth z punktem końcowym api, aby sprawdzić tokeny dostępu. Poniżej znajduje się konfiguracja mojego serwera zasobów.Wiosenna konfiguracja oddzielnego serwera zasobów Oauth2

@Configuration 
@EnableResourceServer 
@EnableWebSecurity 
public class Oauth2SecurityConfiguration extends WebSecurityConfigurerAdapter  { 


@Override 
protected void configure(HttpSecurity http) throws Exception { 
    System.out.println("Oauth2SecurityConfiguration before"); 
    http 
       .authorizeRequests() 
       .antMatchers(HttpMethod.GET, "/api/v1/**").authenticated(); 
    System.out.println("Oauth2SecurityConfiguration after"); 
} 

@Bean 
public AccessTokenConverter accessTokenConverter() { 
    return new DefaultAccessTokenConverter(); 
} 

@Bean 
public RemoteTokenServices remoteTokenServices() { 
    final RemoteTokenServices remoteTokenServices = new RemoteTokenServices(); 
    remoteTokenServices.setCheckTokenEndpointUrl("http://localhost:9000/authserver/oauth/check_token"); 
    remoteTokenServices.setClientId("clientId"); 
    remoteTokenServices.setClientSecret("clientSecret"); 
    remoteTokenServices.setAccessTokenConverter(accessTokenConverter()); 
    return remoteTokenServices; 
} 

@Override 
@Bean 
public AuthenticationManager authenticationManager() throws Exception { 
    OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager(); 
    authenticationManager.setTokenServices(remoteTokenServices()); 
    return authenticationManager; 
} 
} 


@Configuration 
@EnableResourceServer 
public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 
    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 
     System.out.println("http.csrf().disable()"); 
     http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/v1/**").fullyAuthenticated(); 
     System.out.println("http.authorizeRequests().anyRequest().authenticated()"); 
    } 
} 


@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) 
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { 

@Override 
protected MethodSecurityExpressionHandler createExpressionHandler() { 
    return new OAuth2MethodSecurityExpressionHandler(); 
} 
} 

Pytanie: 1. dlaczego AuthenticationManager na serwerze zasobów, podczas gdy wszyscy uwierzytelnianie jest delegowany do dopuszczonego serwer. (Musiałem dodać, aby załadować kontekst aplikacji)

Oprócz tego mam do czynienia z poniższymi problemami.

  1. Mimo że nie przekazuję nagłówków autoryzacji i tokenu dostępu z żądaniem. To przechodzi.

    http GET "http://localhost:8080/DataPlatform/api/v1/123sw/members" 
    HTTP/1.1 200 OK 
    Content-Type: application/json;charset=UTF-8 
    Date: Mon, 19 Oct 2015 19:45:14 GMT 
    Server: Apache-Coyote/1.1 
    Transfer-Encoding: chunked 
    { 
    "entities": [], 
    "errors": [], 
    "message": null 
    } 
    
  2. Filtry są wywoływane tylko od razu nie widzę dzienniki następujące wnioski. Czy gdzieś jest to buforowanie autoryzacji?

Jestem nowy na wiosnę oauth Proszę dać mi znać, jeśli robię coś złego. Używam

spring-security-oauth2 : 2.0.7.RELEASE 
spring-security-core : 4.0.1.RELEASE 
java : 1.8 

Odpowiedz

0

Nie trzeba @EnableWebSecurity na Oauth2SecurityConfiguration@EnableResourceServer wystarczy. Powinieneś również zamienić extends WebSecurityConfigurerAdapter na extends ResourceServerConfigurerAdapter.

Jeśli chcesz użyć instancji RemoteTokenServices polecam zastąpić ResourceServerConfigurerAdapterpublic void configure(ResourceServerSecurityConfigurer resources) throws Exception z

@Override 
public void configure(ResourceServerSecurityConfigurer resources) throws Exception 
{ 
    resources.tokenServices(serverConfig.getTokenServices()); 
} 
+0

Ale widzę „W celu wykorzystania tego filtra należy @EnableWebSecurity gdzieś w aplikacji” w Dokumentach http: //docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/web/configuration/EnableResourceServer.html –