【发布时间】:2021-10-25 13:46:01
【问题描述】:
当我登录时,我会收到 access_token 和 refresh_token,我会在每个 HTTP 请求中将它们作为标头发送到 Spring 引导服务器。
我遇到的问题是,当令牌过期时,当我尝试访问无需身份验证即可访问的路由时,我得到 401 响应,因此结果是我无法访问 /v1/users/current,它检查令牌是否有效,如果不接受刷新令牌并给我一个新的访问令牌。
我现在要做的是使 /v1/users/current 无需任何类型的授权/身份验证即可访问,因此我可以接收新的访问令牌,但我不确定如何使用 KeycloakWebSecurityConfigurerAdapter 来实现。
我尝试使用 HandlerInterceptor,但我收到的 preHandle 请求已经通过 keycloak,我收到类似 401 的错误。
提前致谢!
编辑:当我使用 POSTMAN 并向 /v1/users/current 发送一个没有访问令牌(无身份验证)并且在标头中有刷新令牌的获取请求时,我仍然可以访问 /v1/users/current 路由并获得新的我需要的访问令牌。
@KeycloakConfiguration
class ResourceServerConfiguration extends KeycloakWebSecurityConfigurerAdapter {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
//the below three lines will add the relevant CORS response headers
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<>(new HttpSessionEventPublisher());
}
@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
public KeycloakConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().configurationSource(corsConfigurationSource())
.and()
.authorizeRequests().antMatchers(HttpMethod.GET,"/v1/users/current").permitAll().and()
.authorizeRequests()
.antMatchers(HttpMethod.POST,"/login").permitAll()
.antMatchers(HttpMethod.POST,"/register/admin").hasRole("admin")
.antMatchers(HttpMethod.POST, "/*")
.authenticated()
.anyRequest()
.permitAll();
}
/* @Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.GET,"/v1/users/current");
}*/
}
【问题讨论】:
标签: spring-boot keycloak