2017-08-28 81 views
4

我正在評估一些IAM產品,並且遇到了來自Keycloak的RealmResourceProvider的CORS問題。 目標是編寫一個能夠創建用戶並使用來自Keycloak的REST界面管理組的角客戶端4。keycloak realmresourceprovider corse

服務器端:

我試圖推行以訪問領域和用戶數據儘可能容易與RealmResourceProvider接口REST接口。 我遵循了Beercloak示例(github.com/dteleguin/beercloak)並使其工作,但沒有自定義主題(僅限REST資源)。我自己的應用程序打包成Jar。 我設法通過REST客戶端調用這個Facade並且​​它工作了(通過首先調用localhost:8080/auth/realms/master/protocol/openid-connect/token然後在令牌頭中填充令牌)。

keycloak配置 但是,如果我通過瀏覽器進行測試,我將需要啓用跨來源-的ressource共享。爲了做到這一點,我添加了「啓用-CORS」屬性在服務器應用程序的「keycloak.json」:

{ 
"realm": "master", 
"auth-server-url": "http://localhost:8080/auth", 
"ssl-required": "external", 
"resource": "pharmacyRessource", 
"public-client": true, 
"enable-cors": true 
} 

此外,我創造了Keycloak管理客戶端。 Client Config

客戶端和問題:

角客戶端使用從github.com/mohuk/ng2-keycloak/blob/master/src/keycloak.service.ts到Mohuks NG2-keycloak服務獲取accesstoken。 - 工作正常。 但是,如果我向我的資源發出GET請求,預檢失敗,因爲缺少訪問控制 - 允許原始標頭: Error 401 用於在javascript中初始化keycloak-client的keycloak.json如下所示:

{ 
    "realm": "master", 
    "auth-server-url": "http://localhost:8080/auth", 
    "ssl-required": "external", 
    "resource": "pharmacyRessource", 
    "public-client": true 
} 

我失敗瞭解決方案:

  • 我試圖執行一個CORS過濾器,但我沒有成功,因爲我 無法得到它的keycloak註冊。
  • 我自己也實現了@OPTIONS方法並附加了 的CORSE頭。也沒有工作,因爲方法從未被調用過。
  • 我試圖將它打包爲.war,以便啓用自定義的 過濾器/提供程序,但在註冊資源到keycloak時失敗。

我的測試環境是hub.docker.com/r/jboss/keycloak/

+0

我設法調用OPTIONS方法。但我必須自己將Corse標題添加到所有方法。現在我將根據Requested ClientModel的WebOrigins自行附加標題。 –

回答

1

的官方泊塢窗容器是否需要啓用您的keycloak服務器CORS(在wildlfy運行)。你可以做到這一點,把代碼放入你的standalone.xml中,從wildfly:

 <subsystem xmlns="urn:jboss:domain:undertow:4.0"> 
     <buffer-cache name="default"/> 
     <server name="default-server"> 
      <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/> 
      <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/> 
      <host name="default-host" alias="localhost"> 
       <location name="/" handler="welcome-content"/> 
       <filter-ref name="server-header"/> 
     <filter-ref name="x-powered-by-header"/> 
     <filter-ref name="Access-Control-Allow-Origin"/> 
     <filter-ref name="Access-Control-Allow-Methods"/> 
     <filter-ref name="Access-Control-Allow-Headers"/> 
     <filter-ref name="Access-Control-Allow-Credentials"/> 
     <filter-ref name="Access-Control-Max-Age"/> 
       <http-invoker security-realm="ApplicationRealm"/> 
      </host> 
     </server> 
     <servlet-container name="default"> 
      <jsp-config/> 
      <websockets/> 
     </servlet-container> 
     <handlers> 
      <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/> 
     </handlers> 
      <filters> 
     <response-header name="server-header" header-name="Server" header-value="WildFly/10"/> 
     <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/> 
     <response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="http://localhost"/> 
     <response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/> 
     <response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="accept, authorization, content-type, x-requested-with"/> 
     <response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/> 
     <response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/> 
     </filters> 
    </subsystem>