【问题标题】:How to configure different timeout for different URLs in Spring Oauth2?如何在 Spring Oauth2 中为不同的 URL 配置不同的超时时间?
【发布时间】:2016-04-25 00:13:37
【问题描述】:

假设我配置了两个具有不同 URL 的 API 资源:

  1. /api/secure/**
  2. /api/admin/**
@Override
public void configure(HttpSecurity http) throws Exception {

    http.exceptionHandling()
            .authenticationEntryPoint(customAuthenticationEntryPoint)
            .and()
            .logout()
            .logoutUrl("/oauth/logout")
            .logoutSuccessHandler(customLogoutSuccessHandler)
            .and()
            .csrf()
            .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
            .disable()
            .headers()
            .frameOptions()
            .disable()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/api/secure/**").hasAnyAuthority(Authorities.ROLE_USER.name(), Authorities.ROLE_ADMIN.name())
            .antMatchers("/admin/**").hasAnyAuthority(Authorities.ROLE_ADMIN.name());
}

我配置了超时:

  1. 对于刷新令牌:1 天;
  2. 访问令牌:30 分钟;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("client01")
            .secret("pass")
            .refreshTokenValiditySeconds(24 * 60 * 60)
            .accessTokenValiditySeconds(30 * 60)
            .scopes("read", "write")
            .authorities(Authorities.ROLE_USER.name(), Authorities.ROLE_ADMIN.name(), Authorities.ROLE_SUPERADMIN.name())
            .authorizedGrantTypes("password", "refresh_token");
}

如何为 /api/secure/**(如上)和 /api/admin/**(refreshToken:20 分钟,accessToken:10 秒)设置不同的超时时间?

【问题讨论】:

    标签: spring-boot oauth-2.0 settimeout


    【解决方案1】:

    您可以使用

    添加第二个客户端的配置
    .and()
           .withClient("anotherClient")
           .....
    

    运行时使用的客户端取决于客户端指定的内容:

    http://localhost:8080/oauth/authorize?
     response_type=code
     &client_id=anotherClient
     &redirect_url=http://client_host?key=value
     &scope=read
    

    你可以在implementing oauth2 with spring security找到一个很好的教程

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 2019-11-21
      • 2011-08-29
      • 1970-01-01
      • 2014-08-27
      • 2012-04-05
      • 2020-04-13
      相关资源
      最近更新 更多