启用策略实施器配置时,Keyclock返回403,而不是401,用于未经身份验证的请求。删除策略实施器配置时,返回401。
这个配置我得到一个403空响应.
keycloak:
realm: ${KEYCLOAK_REALM}
auth-server-url: ${KEYCLOAK_AUTH_SERVER_URL}
ssl-required: external
resource: ${KEYCLOAK_CLIENT_ID}
credentials.secret: ${KEYCLOAK_CLIENT_SECRET}
use-resource-role-mappings: true
cors: true
public-client: false
bearer-only: true
policy-enforcer-config:
lazy-load-paths: true
http-method-as-scope: true
path-cache-config:
max-entries: 1000
lifespan: 1000
paths:
- name: Insecure Resource
path: /
enforcement-mode: DISABLED
- name: Swagger UI
path: /swagger-ui/*
enforcement-mode: DISABLED
- name: Swagger Resources
path: /swagger-resources/*
enforcement-mode: DISABLED
- name: Swagger api Resources
path: /api-docs
enforcement-mode: DISABLED
securityConstraints:
- authRoles:
- '*'
securityCollections:
- name: protected
patterns:
- '/v1/*'
- '/intranet/*'
字符串
如果像这样删除策略强制器
keycloak:
realm: ${KEYCLOAK_REALM}
auth-server-url: ${KEYCLOAK_AUTH_SERVER_URL}
ssl-required: external
resource: ${KEYCLOAK_CLIENT_ID}
credentials.secret: ${KEYCLOAK_CLIENT_SECRET}
use-resource-role-mappings: true
cors: true
public-client: false
bearer-only: true
# policy-enforcer-config:
# lazy-load-paths: true
# http-method-as-scope: true
# path-cache-config:
# max-entries: 1000
# lifespan: 1000
# paths:
# - name: Insecure Resource
# path: /
# enforcement-mode: DISABLED
# - name: Swagger UI
# path: /swagger-ui/*
# enforcement-mode: DISABLED
# - name: Swagger Resources
# path: /swagger-resources/*
# enforcement-mode: DISABLED
# - name: Swagger api Resources
# path: /api-docs
# enforcement-mode: DISABLED
securityConstraints:
- authRoles:
- '*'
securityCollections:
- name: protected
patterns:
- '/v1/*'
- '/intranet/*'
型
返回401
{
"timestamp": "2021-10-05T11:25:33.116+0000",
"status": 401,
"error": "Unauthorized",
"message": "No message available",
"path": "/v1/approve-documents"
}
型
对所有请求执行策略,即使请求未经过身份验证或未经过身份验证。如果令牌无效或丢失,如何返回401。
完整代码https://github.com/prajintst/keyclock-permissions
3条答案
按热度按时间0ve6wy6x1#
根据Keycloak架构图,策略执行检查发生在授权/身份验证之前。因此,您无法使用策略强制实现预期的输出。x1c 0d1x的数据
我建议您使用策略评估者/提供者或使用基于角色的授权来实现这一点。
p4rjhz4m2#
从这篇文章中看到我的答案:Spring安全插件应该响应401而不是403
它帮助我正确设置状态代码
8tntrjer3#
一个简单但有效的解决方案。
在你的spring Boot 项目中创建一个包:
字符串
在上面的包中创建一个名为AbstractKeycloakAuthenticatorValve.java的JAVA文件,内容如下:
型
基本上,我们正在修改Keycloak中的AbstractKeycloakAuthenticatorValve文件,以处理失败的身份验证。