2015-03-12 16 views
6

Kaynak sunucuyu, önyükleme sırasında yetkilendirme sunucusundan ayırmaya çalışıyorum. Ayrı ayrı çalıştığım iki farklı uygulama var. Yetkilendirme sunucusunda, oauth/jetondan taşıyıcı belirteci alabilirim ancak kaynağa erişmeyi denediğimde (belirtecini başlığa göndererek) geçersiz bir belirteç hatası alıyorum. Amacım InMemoryTokenStore ve taşıyıcı belirtecini kullanmaktır. Kimse bana kodumda neyin yanlış olduğunu söyleyebilir mi?Spring-boot oauth2 ayırma yetkilendirme sunucusu ve kaynak sunucusu

Yetkilendirme Sunucusu:

@SpringBootApplication 
public class AuthorizationServer extends WebMvcConfigurerAdapter { 

    public static void main(String[] args) { 
    SpringApplication.run(AuthorizationServer.class, args); 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    private TokenStore tokenStore = new InMemoryTokenStore(); 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints 
     .authenticationManager(authenticationManager) 
     .tokenStore(tokenStore); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
     security.checkTokenAccess("hasAuthority('ROLE_USER')"); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients 
      .inMemory() 
      .withClient("user") 
      .secret("password") 
      .authorities("ROLE_USER") 
      .authorizedGrantTypes("password") 
      .scopes("read", "write") 
      .accessTokenValiditySeconds(1800); 
    } 
} 

Kaynak Sunucusu: InMemoryTokenStore 2 örneklerini oluşturduk

@SpringBootApplication 
@RestController 
@EnableOAuth2Resource 
@EnableWebSecurity 
@Configuration 
public class ResourceServer extends WebSecurityConfigurerAdapter { 



public static void main(String[] args){ 
    SpringApplication.run(ResourceServer.class, args); 
} 

@RequestMapping("/") 
public String home(){ 
    return "Hello Resource World!"; 
} 

@Bean 
public ResourceServerTokenServices tokenService() { 
    RemoteTokenServices tokenServices = new RemoteTokenServices(); 
    tokenServices.setClientId("user"); 
    tokenServices.setClientSecret("password"); 
    tokenServices.setTokenName("tokenName"); 
    tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token"); 
    return tokenServices; 
} 

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

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .requestMatchers() 
      .antMatchers("/","/home") 
      .and() 
      .authorizeRequests() 
      .anyRequest().access("#oauth2.hasScope('read')"); 
    } 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     TokenStore tokenStore = new InMemoryTokenStore(); 
     resources.resourceId("Resource Server"); 
     resources.tokenStore(tokenStore); 
    } 
} 
+1

Merhaba, ben de aynı şeyi yapmaya çalışıyorum, lütfen beni takip ettiğiniz/projeyi takip ettiğiniz referansları belirtin –

cevap

6

. Kimlik doğrulama sunucusu ile kaynak sunucu arasında jeton paylaşmak isterseniz, aynı mağazaya ihtiyaç duyarlar.

+0

Teşekkür ederim Dave! – thomasso

+1

@Dave Syer - Bu yüzden kaynak sunucumda InmemoryTokenStore örneğini oluşturmam gerekmiyor mu? –

+0

Hayır, bu hiç yardımcı olmaz mıydı (tüm semboller başka yerde saklanır)? –