首页 > 解决方案 > nodejs Keycloak适配器强制redirect_uri与ssl

问题描述

我有一个带有保护https://example.com的 keycloak 适配器的 nodejs express 应用程序(使用 SSL)

我的 keycloak 适配器是这样配置的: 注意两个 URL 中在此处输入图像描述 的 http S。

现在我面临一个问题,当用户成功登录(通过 keycloak)时,他会看到“无效参数:redirect_uri”错误消息。

使用我在转到https://example.com(使用 SSL)后看到的浏览器开发工具,用户被重定向到https://sso.example.com/auth/realms/myrealm/protocol/openid-connect/auth?client_id =my-client-id&state=22f41ed3-ddc6-4758-970b-d876cf631ded&redirect_uri=http%3A%2F%2Fexample.com%2F%3Fauth_callback%3D1&scope=openid&response_type=code

上面链接中的关键点是redirect_uri=http%3A%2F%2Fexample.com%2F%3Fauth_callback%3D1&scope=openid&response_type=code。在这里我们可以看到redirect_uri没有 SSL。它是 http 而不是 http s

这就是我保护域根的方式:

   const server = express()
   server.set('trust proxy', 'loopback')
   server.use(keycloak.middleware({}))

   const keycloak = new Keycloak({ store: memoryStore }, KEYCLOAK_CONFIG) // KEYCLOAK_CONFIG is the json config below
   (...)
   server.get('/', keycloak.protect('private-user'), (req: Request, res: Response) => {
        // Set the cookie
        res.cookie(ConfigService.CONFIG_COOKIE_KEY, JSON.stringify(ConfigService.CONFIG), { httpOnly: false })
        res.sendFile(path.join(__dirname, 'build', 'index.html'))
        return res
    })

我的keycloak配置是:

{
  realm: 'myrealm',
  'auth-server-url': 'https://sso.example.com/auth/',
  'ssl-required': 'external',
  resource: 'my-client-id',
  'verify-token-audience': false,
  credentials: { secret: 'super-secret-credential' },
  'use-resource-role-mappings': true,
  'confidential-port': 443,
  'bearer-only': false,
  'enable-cors': true,
  'cors-max-age': 1000,
  'enable-basic-auth': false,
  'expose-token': true,
  'disable-trust-manager': false,
  'allow-any-hostname': false,
  'token-minimum-time-to-live': 10,
  'min-time-between-jwks-requests': 10,
  'connection-pool-size': 20,
  'public-key-cache-ttl': 86400
}

redirect_uri必须使用 SSL 。如何强制 keycloak 在 redirect_uri 中使用 http ( SSL)?

http://example.com(非 SSL)添加到Valid Redirect URIs不是一个选项。

标签: node.jskeycloakkeycloak-nodejs-connect

解决方案


推荐阅读