Keycloak、spring 和 /sso/login URL 设置

Mar*_*usz 6 java spring keycloak

我有一个隐藏在反向代理后面的 Spring Boot 应用程序。当用户未经授权向资源发送请求时,他会被重定向到 keycloak 登录页面,redirect_uri参数设置为 例如http://some_url/sso/login(URL 始终相对于上下文路径)。我需要将此 URL 更改为类似的内容http://some_url/api/my_api/sso/login,以便反向代理知道将此类请求重定向到何处。知道如何实现这一目标吗?是否有任何 keycloak 适配器配置选项来设置它(我没有找到任何)或需要覆盖某些 bean?

Mar*_*usz 4

当我设法找到解决方案时,我将回答我的问题(以防将来有人遇到类似的问题)。所以基本上需要两件事:

  1. 通过在请求匹配器中设置自定义 URL 来覆盖 KeycloakAuthenticationProcessingFilter bean:
@Bean
    @Override
    protected KeycloakAuthenticationProcessingFilter keycloakAuthenticationProcessingFilter() throws Exception {
        RequestMatcher requestMatcher =
                new OrRequestMatcher(
                        new AntPathRequestMatcher("/custom_url/sso/login"),
                        new RequestHeaderRequestMatcher("Authorization"),
                        new QueryParamPresenceRequestMatcher("access_token"),
                        new AdapterStateCookieRequestMatcher());

        return new KeycloakAuthenticationProcessingFilter(authenticationManagerBean(), requestMatcher);
    }
Run Code Online (Sandbox Code Playgroud)
  1. 设置正确的身份验证入口点:
@Override
    protected void configure(final HttpSecurity http) throws Exception {
        KeycloakAuthenticationEntryPoint entryPoint = (KeycloakAuthenticationEntryPoint)authenticationEntryPoint();
        entryPoint.setLoginUri("/custom_url/sso/login");

        super.configure(http);
        http
                .authorizeRequests()
                //...
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(entryPoint)
                .and()
                .csrf().disable();


    }
Run Code Online (Sandbox Code Playgroud)