我在一个Java 8应用程序上工作(包括JBOSS, Servlet,没有Spring)。它使用ldap进行身份验证。现在我必须实现saml2机制。为了实现这一点,我使用了Keycloak库(Java Servlet过滤器适配器)。它工作正常,Idp将元数据发送回我的应用程序。
经过身份验证后,用户具有预期的角色,如果我调用方法"isUserInRole",它将返回true。但是@RolesAllowed不接受角色并抛出异常。
@Path("/abcd")
@GET
public Response abcd(@Context final HttpServletRequest httpRequest) {
httpRequest.isUserInRole("user_role"); // true
return Response.noContent().build();
}
@Stateless
public class MyClass {
@RolesAllowed({"user_role"})
public void function() {
// ...
}
// javax.ejb.EJBAccessException: function is not allowed
}
我认为,由于ldap的一些旧配置,RolesAllowed不能正确工作,但我没有找到任何东西。
如果你有任何想法,请写下来。谢谢!
更新:我注意到,如果我删除@无状态,它可以正常工作,但这不是解决方案,至少不适合我。和这里一样的问题,但我不知道如何解决。
一个问题是,@RolesAllowed
将默认期望用户具有以前缀ROLE_
开头的权限。因此,@RolesAllowed({"user_role"})
将期望用户具有ROLE_user_role权限。但是现在由于用户只有user_role
权限,它与ROLE_user_role
不匹配,因此被拒绝访问。
@GetMapping
@PreAuthorize("hasAuthority('user_role')")
public List<User> list(){
return userRepository.findAll();
}
直接检查用户是否有权限user_role,没有任何前缀行为。