1

我目前正在尝试 Glasshfish v3 中的 JDBCRealm:我有 2 个角色 USER 和 ADMIN。

我有一个基于 request.isUserInRole("ADMIN") 方法重定向到 url(比如 /admin 或 /user)的 LoginServlet。

问题是当 ADMIN 登录时它返回 true,因此被重定向到 /admin 但他也可以访问 /user。当用户登录时 request.isUserInRole("ADMIN") 也返回 true。request.isUserInRole("NONEXISTINGROLE") 两者都返回 false。

例如:

request.isUserInRole("ADMIN") +" "+ request.isUserInRole("USER")+" "+ request.isUserInRole("NONEXISTINGROLE")

对于已登录的用户:返回 true true false

登录 ADMIN 返回 true true false

这是我的 web.xml 的一部分:

<security-constraint>
    <display-name>Constraint1</display-name>
    <web-resource-collection>
        <web-resource-name>adminProtected</web-resource-name>
        <description>Administrator restricted area</description>
        <url-pattern>/admin/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>ADMIN</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <display-name>Constraint2</display-name>
    <web-resource-collection>
        <web-resource-name>userProtected</web-resource-name>
        <description>User restricted area</description>
        <url-pattern>/user/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>USER</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <display-name>Constraint3</display-name>
    <web-resource-collection>
        <web-resource-name>LoginServlet</web-resource-name>
        <description>All restricted area</description>
        <url-pattern>/LoginServlet</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>USER</role-name>
        <role-name>ADMIN</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>securityJDBC</realm-name>
    <form-login-config>
        <form-login-page>/login.jsf</form-login-page>
        <form-error-page>/login.jsf</form-error-page>
    </form-login-config>
</login-config>

<security-role>
    <description></description>

    <role-name>USER</role-name>
</security-role>
<security-role>
    <description></description>
    <role-name>ADMIN</role-name>
</security-role>
<servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>controllers.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

还有我的 sun-web.xml:

    <security-role-mapping>
    <role-name>USER</role-name>
    <group-name>USER</group-name>
</security-role-mapping>
<security-role-mapping>
    <role-name>ADMIN</role-name>
    <group-name>ADMIN</group-name>
</security-role-mapping>

谢谢!

4

2 回答 2

2

通过确保领域设置“分配组”为空来修复它。Glassfish 将从组表中加载它们。

于 2010-02-04T15:22:39.470 回答
0

乍一看,您的安全映射看起来不错。你的用户映射怎么样?看起来相同的用户名映射到用户和管理员角色。

于 2009-12-28T14:22:05.357 回答