需要完全身份验证才能使用 Spring 安全和 Keycloak 访问此资源
Full authentication is required to access this resource with Spring Security and Keycloak
我正在尝试使用 Keycloak 配置我的 Spring 安全性。我正在使用 Spring Boot。我的 pom 中有以下依赖项。
<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>
我使用 spring 引导版本 1.5.7.RELEASE。和keycloak版本3.2.1.Final
以及我的 application.properties 中的以下属性:
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"
}
您没有在此处使用 Keycloak 配置 Spring 安全性,security-constraints
用于保护 servlet 容器并且与 Spring 安全性无关。使用 SpringSecurity 时不需要它,添加一个 Secuirty Config class extending KeycloakWebSecurityConfigurerAdapter
,检查这里:http://www.keycloak.org/docs/3.3/securing_apps/topics/oidc/java/spring-security-adapter.html
我正在尝试使用 Keycloak 配置我的 Spring 安全性。我正在使用 Spring Boot。我的 pom 中有以下依赖项。
<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>
我使用 spring 引导版本 1.5.7.RELEASE。和keycloak版本3.2.1.Final 以及我的 application.properties 中的以下属性:
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"
}
您没有在此处使用 Keycloak 配置 Spring 安全性,security-constraints
用于保护 servlet 容器并且与 Spring 安全性无关。使用 SpringSecurity 时不需要它,添加一个 Secuirty Config class extending KeycloakWebSecurityConfigurerAdapter
,检查这里:http://www.keycloak.org/docs/3.3/securing_apps/topics/oidc/java/spring-security-adapter.html