【问题标题】:Keycloak: refresh access token after expired for ajax callsKeycloak:ajax调用过期后刷新访问令牌
【发布时间】:2018-12-08 01:13:34
【问题描述】:

我面临来自herehere 的相同问题,但他们没有得到答复,我有更多信息和不同的设置,因此不会重复。

我有一个 spring boot 1.5.13 应用程序,使用带有 keycloak 3.4.3 服务器的 spring 安全适配器。一切正常,但是当我在 5 分钟后向应用程序发出 ajax 请求而不重新加载页面时,响应返回 401 错误。我知道这是因为访问令牌已过期。

文档说明如下:

令牌最小生存时间 在过期之前使用 Keycloak 服务器抢先刷新活动访问令牌的时间量(以秒为单位)。当访问令牌被发送到另一个 REST 客户端时,这在评估之前可能会过期,这尤其有用。此值不应超过领域的访问令牌生命周期。这是可选的。默认值为 0 秒,因此适配器会在访问令牌过期时刷新它。

文档here

我已经更改了 keycloak.json 中 token-minimum-time-to-live 的默认值,但我不起作用。

{
"realm": "APPS",
"auth-server-url": "http://localhost:9100/auth",
"ssl-required": "external",
"resource": "WebApp",
"public-client": true,
"confidential-port": 0,
"use-resource-role-mappings": true,
"principal-attribute":"preferred_username",
"token-minimum-time-to-live" : 15
}

所以我认为我在 spring 安全适配器配置中遗漏了一些东西:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws   
Exception {

KeycloakAuthenticationProvider keycloakAuthenticationProvider = 
keycloakAuthenticationProvider();

keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new 
SimpleAuthorityMapper());
    auth.authenticationProvider(keycloakAuthenticationProvider);
}

@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
    return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}

@Bean
ServletListenerRegistrationBean<HttpSessionEventPublisher> getHttpSessionEventPublisher() {
    return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}



@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http
    .logout()
    .logoutRequestMatcher(new AntPathRequestMatcher("/sso/logout")) 
    .and()
    .authorizeRequests()
    .antMatchers("/Portal/**").hasRole("App_Access")
    .anyRequest().permitAll()                
    .and()
    .headers().frameOptions().sameOrigin()
    ;
  }
}

【问题讨论】:

    标签: ajax spring-boot spring-security keycloak


    【解决方案1】:

    问题是,即使你延长了这段时间,在更长的时间之后你仍然会面临这个问题。

    如果您创建的每个 ajax 请求都通过“自动刷新/重新加载”您的页面来对那些 401 做出本机反应,您会选择这种方式吗?如果您的令牌仍然可以刷新,它将被适配器反向通道请求自动刷新到身份验证服务器(在页面刷新之后);如果由于您超过了刷新令牌的有效期限而无法刷新令牌,那么您的应用程序将被重定向到@auth 服务器用户登录页面。

    这对你有帮助吗?在这种情况下,您可能需要稍微修改您的页面,以便在 401 ajax 请求后重新加载页面后,“失败的请求”会在页面重新加载后自动重复(只有这次 access_token 将被刷新或完全过期) .

    如果一切都按预期工作,最终用户要么不会注意到页面重新加载并且一切都会照常工作,要么他将被重定向到凭据输入页面。

    希望对你有帮助

    编辑:

    至于“token-minimum-time-to-live”等token配置选项,你有没有直接在keycloak管理页面直接尝试@Realm->settings->Tokens?

    我相信这个其他问题与您的问题有关并提供了一种类似的解决方案:Spring Security + Keycloak - How to handle Ajax Requests in conjunction with "Access Token Lifespan"

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 2018-06-05
      • 2019-05-31
      • 2021-10-03
      • 2019-04-07
      • 1970-01-01
      • 2021-11-16
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多