2017-09-23 8 views
0

スプリングセキュリティKeycloakに設定しようとしています。私はスプリングブートを使用しています。私は私のポンに次の依存関係があります。SpringセキュリティとKeycloakでこのリソースにアクセスするには完全認証が必要です

<dependency> 
    <groupId>org.keycloak</groupId> 
    <artifactId>keycloak-spring-boot-starter</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.keycloak</groupId> 
    <artifactId>keycloak-spring-security-adapter</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.keycloak</groupId> 
    <artifactId>keycloak-tomcat8-adapter</artifactId> 
</dependency> 

私は春のブートバージョン1.5.7.RELEASEを使用しています。そして、私のapplication.propertiesでkeycloakバージョン3.2.1.Final し、次のプロパティ:

keycloak.enabled=true 
keycloak.realm=test 
keycloak.auth-server-url=http://localhost:8080/auth 
keycloak.ssl-required=external 
keycloak.resource=rest 
keycloak.bearer-only=true 
keycloak.credentials.secret=<secret> 
keycloak.principal-attribute=preferred_username 
keycloak.security-constraints[0].authRoles[0]=application 
keycloak.security-constraints[0].securityCollections[0].name=spring secured api 
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/api/** 

management.security.enabled=false 

は、私は他の構成を有していません。私のエンドポイントではJAX-RSを使用します。 私のトークンをリクエストするには私はChrome Application Postmanを使用します。ここでは、要求は次のとおりです。

POST /auth/realms/test/protocol/openid-connect/token HTTP/1.1 
Host: localhost:8080 
Cache-Control: no-cache 
Postman-Token: <postman token> 
Content-Type: application/x-www-form-urlencoded 

grant_type=password&client_id=postman&username=root&password=12345678 

そして、私のアプリケーションの要求:

GET /api/product HTTP/1.1 
Host: localhost:18888 
Authorization: Bearer <keycloak token from the request above> 
Cache-Control: no-cache 
Postman-Token: <postman token> 

しかし、私の応答は次のとおりです。

{ 
    "timestamp": <timestamp>, 
    "status": 401, 
    "error": "Unauthorized", 
    "message": "Full authentication is required to access this resource", 
    "path": "/api/product" 
} 

答えて

0

あなたがここにKeycloakと春のセキュリティを設定していない、security-constraintsをするために使用されていますサーブレットコンテナを保護し、Spring Securityとは無関係です。 SpringSecurityを使用する際には必要ありません。KeycloakWebSecurityConfigurerAdapterに拡張されたSecuirty Configクラスを追加してください。http://www.keycloak.org/docs/3.3/securing_apps/topics/oidc/java/spring-security-adapter.html

+0

ありがとうございます。 Springプロパティファイルを使用する代わりに、 "keycloak.json"を使用することを好むという問題があります。アプリケーションのロード時に、keycloak.jsonがロードされます。しかし、私が休憩をとろうとすると、設定で 'realm'を設定する必要があるという例外があります。手伝って頂けますか?その場合は –

+0

で、Springブートアダプタを使用せず、Spring Securityアダプタを使用し、 'keycloak.json'領域が指定されていることを確認してください。 –

関連する問題