我在here和here中遇到了相同的问题,但是他们没有得到回答,因此我获得了更多信息和不同的设置,因此不再重复。
我有一个spring boot 1.5.13应用程序,它使用带有keycloak 3.4.3服务器的spring安全适配器。一切正常,但是当我在5分钟后向应用程序发出ajax请求而不重新加载页面时,响应返回401错误。我知道这是因为访问令牌已过期。
文档规定以下内容:
令牌生存时间最少
在Keycloak服务器过期之前抢先刷新活动访问令牌的时间(以秒为单位)。当访问令牌发送到另一个REST客户端,在评估之前它可能过期时,这特别有用。此值不得超过域的访问令牌寿命。这是可选的。默认值为0秒,因此适配器将在访问令牌过期时刷新它。
我在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 getHttpSessionEventPublisher() {
return new ServletListenerRegistrationBean(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()
;
}
}