【问题标题】:Spring-boot oauth2 splitting authorization server and resource serverspring-boot oauth2 拆分授权服务器和资源服务器
【发布时间】:2015-05-14 06:26:12
【问题描述】:

我试图在 spring-boot 中将资源服务器与授权服务器分开。我有两个不同的应用程序,我分别运行。在授权服务器中,我可以从 oauth/token 获取不记名令牌,但是当我尝试访问资源(在标头中发送令牌)时,我收到了无效令牌错误。我的意图是使用 InMemoryTokenStore 和不记名令牌。谁能告诉我我的代码有什么问题?

授权服务器:

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

【问题讨论】:

  • 嗨,我也在尝试做同样的事情,请您提供您正在关注/遵循的参考资料来设置项目

标签: java oauth-2.0 spring-boot spring-security-oauth2


【解决方案1】:

您已经创建了 2 个InMemoryTokenStore 实例。如果您想在身份验证服务器和资源服务器之间共享令牌,它们需要相同的存储。

【讨论】:

  • @Dave Syer - 所以我不需要在我的资源服务器中创建 InmemoryTokenStore 实例?
  • 不,这根本没有帮助(所有令牌都存储在其他地方)?
  • @DaveSyer 是否有可能实现这一点,而不是将 2 个服务器拆分为 2 个应用程序。将其全部集成到一个应用程序中,并在 app.properties 文件中拥有凭据?
  • @DaveSyer,为资源服务器配置clientId/secret 以通过传递令牌从oauth 服务器询问用户信息是否正确(在这种情况下不需要数据库存储访问)?
猜你喜欢
  • 2018-08-29
  • 2022-08-03
  • 2016-05-21
  • 2014-07-09
  • 2019-10-27
  • 2019-04-05
  • 2019-04-17
  • 2022-11-23
  • 2021-10-29
相关资源
最近更新 更多