我用Keycloak确保了我的休息api。身份验证后,当我尝试访问rest API时,我得到:
403错误。无法访问rest api。理由!角色。
在配置中,我将角色指定为*:
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
context.setSecurityHandler(securityHandler);
securityHandler.addRole("*");
ConstraintMapping constraintMapping = new ConstraintMapping();
constraintMapping.setPathSpec("/*");
Constraint constraint = new Constraint();
constraint.setAuthenticate(true);
constraint.setRoles(new String[]{"*"});
我是否假设使用' *' 错误来指定任何角色,或错误是否意味着不同?
我在JettyKeycloakAuthentciator日志中看到以下日志:
2018-05-24 12:55:52,253 [DEBUG] [ers.PreAuthActionsHandler(handleRequest)] - adminRequest http://localhost:7100/api/v1/design/test 2018-05-24 12:55:52,254 [DEBUG] [TokenRequestAuthenticator(thenticateToken)] - 验证 access_token 2018-05-24 12:55:52,255 [DEBUG] [TokenRequestAuthenticator(thenticateToken)] - 成功授权 2018-05-24 12:55:52,255 [DEBUG] [JettyRequestAuthenticator(rAuthentication)] - 完成承载 认证。持票人角色:[uma_authorization] 2018-05-24 12:55:52,255 [DEBUG] [ters.RequestAuthenticator(eAuthentication)] - 用户' 8f9381df-2f7e-4ff8-9ef5-2123b03db3c9'调用 ' http://localhost:7100/api/v1/design/test'在客户端' my_server' 2018-05-24 12:55:52,255 [DEBUG] [ters.RequestAuthenticator(authenticate)] - 持有人AUTHENTICATED 2018-05-24 12:55:52,255 [DEBUG] [thenticatedActionsHandler(processedRequest)] - AuthenticatedActionsValve.invoke http://localhost:7100/api/v1/design/test
答案 0 :(得分:1)
确定设置任何角色的正确方法是:
constraint.setRoles(new String[]{"**"});
即。 加倍* 而不是单*
另外,我删除了这一行:
securityHandler.addRole("*");