【问题标题】:KeyCloak Spring Boot - Add custom code on auth successKeyCloak Spring Boot - 在验证成功时添加自定义代码
【发布时间】:2021-10-21 12:43:21
【问题描述】:

我在 this 指南中使用 KeyCloak 与 Spring Boot 的集成。我的安全配置如下:

class KeycloakSecurityConfiguration extends KeycloakWebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests().antMatchers("/**").authenticated()
            .anyRequest().permitAll();
    }
}

我想在 KeyCloak 将我重定向到实际资源之前为 onAuthenticationSuccess 添加一些自定义代码。我尝试使用AuthenticationSuccessHandler 实现自定义类并执行formLogin().successHandler(...)。这没有用。我怎样才能让它工作??

【问题讨论】:

  • 您使用的是什么版本的 Spring Security?这是一个新项目吗?如果是这样,我建议不要使用keycloak-spring-boot-starter,而是使用内置支持 oauth2 客户端的 Spring Security 5.5。请参阅spring-security-keycloak-demo 以获取示例项目以开始使用。如果您希望使用 OAuth2 登录,则需要使用 .oauth2Login((oauth2Login) -> oauth2Login.successHandler(...)) 而不是 .formLogin(...)
  • 只是想了解为什么不使用 keycloak 启动器?你能详细说明一下吗??
  • 当然。 1) keycloak starter 不是由 Spring Security 社区维护的,因此是一个特定于 keycloak 的实现,所以你不能真正从 spring security 社区获得太多帮助,2) spring security 适配器 pin 一个旧版本的 spring security (最新的引脚到 5.2.9,但最新的 5.2.x 是 5.2.12)所以你可能不会得到最新的安全补丁,并且 3)它的大部分/所有功能似乎都被更高版本的 spring security 所包含。因此,我只推荐使用spring security。当然,这是您的选择,使用 keycloak 可能有充分的理由。
  • @SteveRiesenberg 如何在两个库中的任何一个中生成 JWT 令牌?
  • Here's an example of how generate JWTs using Nimbus 在 spring-security-samples 项目中。

标签: spring-boot spring-security keycloak


【解决方案1】:

如果您仍然喜欢使用 Spring Boot KeyCloak,类似这样的方法也可以。

public class KeyCloakAuthSuccessHandler extends KeycloakAuthenticationSuccessHandler {


    public KeyCloakAuthSuccessHandler(AuthenticationSuccessHandler fallback) {
        super(fallback);
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        if (authentication.getPrincipal() instanceof KeycloakPrincipal) {
            AccessToken token = ((KeycloakPrincipal<?>) authentication.getPrincipal()).getKeycloakSecurityContext().getToken();
        }
        super.onAuthenticationSuccess(request, response, authentication);
    }
}

并在扩展 KeyCloakWebSecurityConfigurerAdapter 的安全配置或类似文件中执行以下操作:

@Bean
@Override
protected KeycloakAuthenticationProcessingFilter keycloakAuthenticationProcessingFilter() throws Exception {
    KeycloakAuthenticationProcessingFilter filter = new KeycloakAuthenticationProcessingFilter(authenticationManagerBean());
    filter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy());
    filter.setAuthenticationSuccessHandler(successHandler());
    return filter;
}


@NotNull
@Bean
public KeyCloakAuthSuccessHandler successHandler() {
    return new KeyCloakAuthSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler());
}

【讨论】:

  • 对我帮助很大!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 2021-06-24
  • 2017-06-28
  • 2019-02-25
  • 2016-06-30
  • 2017-03-17
相关资源
最近更新 更多