2015-03-12 21 views
6

मैं वसंत-बूट में प्राधिकरण सर्वर से संसाधन सर्वर को विभाजित करने का प्रयास कर रहा हूं। मेरे पास दो अलग-अलग एप्लिकेशन हैं जिन्हें मैं अलग से चला रहा हूं। प्राधिकरण सर्वर में मैं oauth/टोकन से भालू टोकन प्राप्त कर सकता हूं लेकिन जब मैं संसाधन तक पहुंच प्राप्त करने का प्रयास कर रहा हूं (शीर्षलेख में टोकन भेज रहा हूं) मुझे एक अवैध टोकन त्रुटि मिल रही है। मेरा इरादा इनमेमरी टोकनस्टोर और भालू टोकन का उपयोग करना है। क्या कोई मुझे बता सकता है कि मेरे कोड में क्या गलत है?स्प्रिंग-बूट oauth2 विभाजन प्राधिकरण सर्वर और संसाधन सर्वर

प्राधिकरण सर्वर:

@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); 
    } 
} 

संसाधन सर्वर:

@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

हाय, मैं भी वही काम करने की कोशिश कर रहा हूं, कृपया मुझे संदर्भ दें जो आप निम्नलिखित हैं/परियोजना –

उत्तर

6

आप InMemoryTokenStore के 2 उदाहरणों बनाया है। यदि आप ऑथ सर्वर और संसाधन सर्वर के बीच टोकन साझा करना चाहते हैं तो उन्हें एक ही स्टोर की आवश्यकता है।

+0

सेटअप करने के लिए निम्नलिखित हैं, धन्यवाद डेव! – thomasso

+1

@ डेव सीयर - तो मुझे अपने संसाधन सर्वर में InmemoryTokenStore का उदाहरण बनाने की आवश्यकता नहीं है? –

+0

नहीं, इससे कोई मदद नहीं होगी (सभी टोकन कहीं और संग्रहीत हैं)? –

संबंधित मुद्दे