代码之家  ›  专栏  ›  技术社区  ›  Michael Bavin

request.isUserInRole(“admin”)还为不在角色admin中但在角色user中的用户返回true

  •  1
  • Michael Bavin  · 技术社区  · 16 年前

    我目前正在Glasshfish v3中尝试JDBCrealm: 我有两个角色用户和管理员。

    我有一个基于request.isUserInRole(“admin”)方法重定向到url(例如/admin或/user)的loginslet。

    问题是,当一个管理员登录时,它返回true,因此被重定向到/admin,但他也可以访问/user。当用户登录请求时,isUserInRole(“admin”)也返回true。request.isUserInRole(“nonexistingRole”)对两者都返回false。

    如:

    request.isUserInRole(“admin”)+“”+ request.isUserInRole(“用户”)+“”+ request.isUserInRole(“不存在的角色”)

    对于loggedin用户:返回true true false

    对于loggedin,admin返回true 真假

    这是我的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>
    

    谢谢您!

    2 回复  |  直到 16 年前
        1
  •  2
  •   Michael Bavin    16 年前

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

        2
  •  0
  •   BalusC    16 年前

    乍一看,您的安全映射看起来很好。您的用户映射如何?看起来相同的用户名同时映射在用户和管理角色上。

    推荐文章