2015-03-12 15 views
6

Nel tentativo di dividere il server di risorse dal server di autorizzazione in avvio di primavera. Ho due diverse applicazioni che sto eseguendo separatamente. Nel server di autorizzazione posso ottenere il token bearer da oauth/token ma quando sto cercando di ottenere l'accesso alla risorsa (inviando il token nell'intestazione) sto ricevendo un errore di token non valido. La mia intenzione è utilizzare InMemoryTokenStore e il token bearer. Qualcuno può dirmi cosa c'è di sbagliato nel mio codice?Server di autorizzazione splitter oauth2 Spring-boot e server di risorse

Autorizzazione Server:

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

Server Resource:

@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

Ciao, sto anche cercando di fare la stessa cosa, per favore, forniscimi i riferimenti che segui/segui per impostare il progetto –

risposta

6

È stato creato 2 istanze di InMemoryTokenStore. Se vuoi condividere i token tra il server di autenticazione e il server delle risorse, hanno bisogno dello stesso negozio.

+0

Grazie Dave! – thomasso

+1

@Dave Syer - Quindi non è necessario creare l'istanza di InmemoryTokenStore nel mio server delle risorse? –

+0

No, non sarebbe affatto d'aiuto (tutti i token sono memorizzati altrove)? –

Problemi correlati