【问题标题】:How to set redirect_uri in Keycloak with Spring boot如何使用 Spring Boot 在 Keycloak 中设置 redirect_uri
【发布时间】:2019-06-07 09:40:20
【问题描述】:

我在我的 Spring Boot 应用程序中使用 Keycloak,但我不知道如何提供我自己的重定向 uri

这是我的配置

keycloak:
    use-resource-role-mappings: true
    realm: customer-realm
    resource: web-frontend
    token-minimum-time-to-live: 30
    principal-attribute: preferred_username
    credentials:
        secret: 321321321-88a2-424c-bb4c-2312312321
    auth-server-url: http://auth.customer.mydomain.tld:9080/auth

我已尝试通过以下方式设置redirect_uri

   <a href="/login?redirect_uri=http://localhost:8443/customer/account">Login</a>

当然,我已将此 URI 添加到我的领域设置中。

但是当用户点击链接时,它看起来如下

http://auth.customer.mydomain.tld:9080/auth/realms/customer-realm/protocol/openid-connect/auth?response_type=code&client_id=web-frontend&redirect_uri=http%3A%2F%2Flocalhost%3A8443%2Fsso%2Flogin&state=e81ad405-8a83-4e84-a155-2f1275f4390b&login=true&scope=openid

如何提供自己的重定向 URI?

谢谢

【问题讨论】:

    标签: java spring spring-boot keycloak keycloak-services


    【解决方案1】:

    我不知道这个问题是否仍然相关,但我偶然发现了同样的问题。尽管有可能通过配置keycloak.redirect-rewrite-rules.pathOld=pathNew 重写部分URI,但这并不适用于权限,即域名。 但是redirect-uri 的域名取自请求的Host 标头,因此适当设置此标头就足够了,例如在代理中。

    或者,您需要注入一个自定义 PostProcessor,它最终注入一个子类 OAuthRequestAuthenticator 并覆盖 getRequestUrl()。草图代码看起来像这样:

    @Component
    public class KeycloackAuthenticationProcessingFilterPostProcessor implements BeanPostProcessor {
    
        private static final Logger logger = LoggerFactory.getLogger(KeycloackAuthenticationProcessingFilterPostProcessor.class);
    
        private void process(KeycloakAuthenticationProcessingFilter filter) {
            filter.setRequestAuthenticatorFactory(new SpringSecurityRequestAuthenticatorFactory() {
                @Override
                public RequestAuthenticator createRequestAuthenticator(HttpFacade facade, HttpServletRequest request, KeycloakDeployment deployment, AdapterTokenStore tokenStore, int sslRedirectPort) {
                    return new SpringSecurityRequestAuthenticator(facade, request, deployment, tokenStore, sslRedirectPort) {
    
                        @Override
                        protected OAuthRequestAuthenticator createOAuthAuthenticator() {
                            return new OAuthRequestAuthenticator(this, facade, deployment, sslRedirectPort, tokenStore) {
    
                                @Override
                                protected String getRequestUrl() {
                                    return "http://localhost:8080";
                                }
                            };
                        }
    
                    };
                }
            });
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof KeycloakAuthenticationProcessingFilter) {
                logger.info("Injecting Custom handler...");
                process(((KeycloakAuthenticationProcessingFilter) bean));
            }
            return bean;
        }
    }
    

    因为我有一个代理,所以我采取了简单的方法,并相应地调整了 Host 标头。

    【讨论】:

      【解决方案2】:

      我们有一个 spring boot 应用程序,它通过配置进行设置:

      @Configuration
      public class Config {
          @Value("${app.client.id}")
          private String clientId;
      
          @Value("${app.client.secret}")
          private String clientSecret;
      
          @Value("${auth.server.hostname}")
          private String authServerHost;
      
          @Value("${auth.server.port}")
          private String authServerPort;
      
          @Bean
          public SecurityConfiguration securityInfo() {
              Map<String, Object> additionalQueryStringParams = new HashMap<>();
              additionalQueryStringParams.put("redirect_uri", 
                "http://" + authServerHost + ":" + authServerPort + "/sso/login");
              return new SecurityConfiguration(clientId, clientSecret, "", "", "", additionalQueryStringParams, false);
          }
      }
      

      也许这也适合你?

      【讨论】:

        猜你喜欢
        • 2021-10-27
        • 2020-04-02
        • 2021-01-07
        • 2019-04-08
        • 2019-04-04
        • 2022-11-25
        • 2018-05-31
        • 2021-07-22
        • 2021-08-11
        相关资源
        最近更新 更多