安全角色映射不适用于文件描述符
将 glassfish 3.1.1 用于 Java EE6 项目时,glassfish-web.xml
中定义的安全角色映射对“用户 - 角色”映射没有影响。
调用 request.isUserInRole("USER")
以及 request.isUserInRole("ADMIN")
始终返回 false
。
glassfish-web.xml
<glassfish-web-app>
<security-role-mapping>
<role-name>ADMIN</role-name>
<group-name>ADMIN</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>USER</role-name>
<group-name>USER</group-name>
</security-role-mapping>
</glassfish-web-app>
使用 @DeclareRoles
注释 LoginBean.java
,如下所示,角色按预期分配。
LoginBean.java
...
@DeclareRoles({"ADMIN", "USERS"})
@Named(value = "loginBean")
@RequestScoped
public class LoginBean implements Serializable { ...
为什么我需要 LoginBean.java
中的 @DeclareRoles
才能为 request.isUserInRole
获取有效的“用户 - 角色”映射>?
Using glassfish 3.1.1 for a Java EE6 project the security role mapping as defined in glassfish-web.xml
has no influence on the 'user - role' mapping.
Calling request.isUserInRole("USER")
as well as request.isUserInRole("ADMIN")
always returns false
.
glassfish-web.xml
<glassfish-web-app>
<security-role-mapping>
<role-name>ADMIN</role-name>
<group-name>ADMIN</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>USER</role-name>
<group-name>USER</group-name>
</security-role-mapping>
</glassfish-web-app>
Annotating LoginBean.java
with @DeclareRoles
as shown below, the roles are assigned as expected.
LoginBean.java
...
@DeclareRoles({"ADMIN", "USERS"})
@Named(value = "loginBean")
@RequestScoped
public class LoginBean implements Serializable { ...
Why do I need the @DeclareRoles
in LoginBean.java
in order to get a working 'user - role' mapping for request.isUserInRole
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Coderanch 上的类似问题 引用17.2.5.3 Bean 代码引用的安全角色声明
EJB 3.1 规范:
(强调我的)
所以这只是对部署者的一个简单提示,他们不必解释代码来获取已使用角色的列表。如果开发人员使用来自其他方法或非常复杂的逻辑的角色名称调用 isUserInRole() 方法,这可能会非常困难。
这也可能很有用(来自17.3 Bean 提供者和/或应用程序组装者的职责):
,但我认为背后的推理是相同的,并且 servlet 规范不是那么详细。)
来自部署者的职责:安全角色的分配 (17.4 .2):
因此,根据规范,
glassfish-web.xml
是由部署者(不是 Bean 提供者或应用程序组装者)创建的,对于部署者的工作,他需要来自“DeclareRoles<”的角色名称。 /code> 和
RolesAllowed
元数据注释和/或部署描述符的security-role
元素。”A similar question on Coderanch cites 17.2.5.3 Declaration of Security Roles Referenced from the Bean’s Code
of the EJB 3.1 specification:
(Emphasis mine)
So it's just a simple hint to the Deployer and they don't have to interpret the code to get the list of the used roles. It could be really hard if a developer calls the
isUserInRole()
method with a role name which comes from an other method or from a very complex logic.This also could be useful (from 17.3 Responsibilities of the Bean Provider and/or Application Assembler):
(I see that the question is about a web application but I think the reasoning behind is the same and servlet spec isn't so detailed.)
From Deployer’s Responsibilities: Assignment of Security Roles (17.4.2):
So, according to the spec the
glassfish-web.xml
is created by the Deployer (not the Bean Provider or Application Assembler) and for the Deployer's work he needs the role names from "DeclareRoles
andRolesAllowed
metadata annotations and/orsecurity-role
elements of the deployment descriptor."glassfish-web.xml 中的角色映射将 Java EE 应用程序的安全角色名称转换为部署环境用户/组机制。角色是抽象的......并且在您的应用程序使用角色之前,映射是不必要的并且不会被咨询。
The role-mapping in the glassfish-web.xml translates a Java EE application's security role names into a deployment environments user/group mechanism. The roles are abstract... and until your application uses a role, the mapping is unnecessary and not consulted.