【问题标题】:Redirecting to two different welcome-pages depending on the users role in Java EE 6/Glassfish根据 Java EE 6/Glassfish 中的用户角色重定向到两个不同的欢迎页面
【发布时间】:2012-03-17 07:23:42
【问题描述】:

我已经使用 Glassfish 3.1 + JDBCRealm + MySQL (MD5) 实现了基于 FORM 的身份验证。我只有两个角色,用户和管理员。一切都很顺利,我可以从日志中看到身份验证在两种情况下都可以作为用户和管理员工作(下面的观看日志)

Q1:是否可以制作两个不同的索引文件,这样当用户是管理员时,他/她去 /admin/index.xhtml,而当用户是角色用户时,他直接去到 faces/user/index.xhtml?

Q2:现在当我以用户身份登录时,我仍然可以进入“管理端”,只需将整个链接直接写入浏览器中的地址字段,为什么要避免这种情况?

Q3:当我以用户身份登录并且欢迎文件列表中只有 faces/admin/index.xhtml 时,即使 xml 文件告诉其他内容,它也会将我重定向到该文件,为什么?

<welcome-file-list>
        <welcome-file>faces/admin/index.xhtml</welcome-file> *?? ----> it goes always here, cause it is the first one I think?*
       <welcome-file>faces/user/index.xhtml</welcome-file>
    </welcome-file-list>

    <security-constraint>
        <display-name>Admin Pages</display-name>
        <web-resource-collection>
            <web-resource-name>Protected Admin Area</web-resource-name>
            <description/>
            <url-pattern>/faces/admin/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
            <http-method>HEAD</http-method>
            <http-method>PUT</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <display-name>User Pages</display-name>
        <web-resource-collection>
            <web-resource-name>Protected Users Area</web-resource-name>
            <description/>
            <url-pattern>/faces/users/*</url-pattern>
            <!--url-pattern>/faces/users/index.xhtml</url-pattern-->
            <http-method>GET</http-method>
            <http-method>POST</http-method>
            <http-method>HEAD</http-method>
            <http-method>PUT</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>JDBCRealm</realm-name>
        <form-login-config>
            <form-login-page>/faces/loginForm.xhtml</form-login-page>
            <form-error-page>/faces/loginError.xhtml</form-error-page>
        </form-login-config>

    </login-config>
</web-app>

日志:

FINE: Login module initialized: class com.sun.enterprise.security.auth.login.JDBCLoginModule
FINEST: JDBC login succeeded for: admin groups:[admin, user]
FINE: JAAS login complete.
FINE: JAAS authentication committed.
FINE: Password login succeeded for : admin
FINE: Set security context as user: admin
FINE: [Web-Security] Setting Policy Context ID: old = null ctxID = jdbcrealm/jdbcrealm
FINE: [Web-Security] hasUserDataPermission perm: (javax.security.jacc.WebUserDataPermission  GET)
FINE: [Web-Security] hasUserDataPermission isGranted: true
FINE: [Web-Security] Policy Context ID was: jdbcrealm/jdbcrealm
FINE: [Web-Security] Codesource with Web URL: file:/jdbcrealm/jdbcrealm
FINE: [Web-Security] Checking Web Permission with Principals : null

(在 myfear 的回答后编辑) -----在 glassfish-web.xml 我有这样的角色。如果我理解正确,这意味着管理员属于组:管理员、客户和用户。客户属于组:客户和用户,用户属于组用户。我理解正确吗?

    <security-role-mapping>
    <role-name>admin</role-name>
    <group-name>admin</group-name>
    <group-name>customer</group-name>
    <group-name>user</group-name>
  </security-role-mapping>
  <security-role-mapping>
    <role-name>customer</role-name>
    <group-name>customer</group-name>
    <group-name>user</group-name>
  </security-role-mapping>
  <security-role-mapping>
    <role-name>user</role-name>
    <group-name>user</group-name>
  </security-role-mapping>

</glassfish-web-app>

谢谢! 萨米人

【问题讨论】:

    标签: glassfish web.xml jdbcrealm redirect


    【解决方案1】:

    我刚刚在大学课程中尝试过此功能,这就是我如何获得我认为您所追求的功能的方法。 我正在使用带有 Glassfish 4.1.1 服务器的 Netbeans,并且已经在服务器文件领域中配置了用户角色。

    我的项目有 3 个文件:

    index.xhtml users/mainmenu.xhtml admin/mainmenu.xhtml

    欢迎页面设置为index.xhtml,并带有以下超链接:

     <h4>
         <a href="/ED-Secure-war/faces/admin/mainmenu.xhtml">
             Admin Login
         </a>
     </h4>
     <h4>
         <a href="/ED-Secure-war/faces/user/mainmenu.xhtml">
               User Login
            </a>
     </h4>
    

    In my web.xml security section I have the following roles configured

    现在,由于通过用户组限制对其中每一个的访问,当您单击索引上的超链接时,系统会提示您登录。如果您为管理员链接输入有效的管理员登录名,您将被重定向到admin/mainmenu.xhtml,反之亦然,以获取用户登录名。

    【讨论】:

      【解决方案2】:

      A1) 欢迎文件与角色无关。如果你需要为调度用户做任何类型的逻辑,你需要考虑使用 boolean HttpServletRequest.isUserInRole(String role) 或类似的东西来找出用户所在的角色。

      A2) 这不应该发生。您需要检查您在 JDBCRealm 中的角色。就我在这里看到的而言,一切都以正确的方式配置。

      A3) 我不确定我是否正确理解了您的备注“XML”文件。但是欢迎文件不绑定到角色和.. 见 A1)

      谢谢, M

      【讨论】:

      • 谢谢我的恐惧!所以它应该像我想的那样工作。如果你有 /faces/admin/*admin 除了管理员中的人之外没有其他人-角色应该进入?在我的情况下,所有经过身份验证的人都可以进入。我稍微更新了问题。
      【解决方案3】:

      对于您的问题 1:使用过滤器,您可以将用户重定向到 userlogin.xhtml 或 adminlogin.xhtml 的特定页面

      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          String userName = SecurityAssociation.getPrincipal().getName();
          String userNameSubject = SecurityAssociation.getSubject().toString();
      
          System.out.println("Yeeey! Get me here and find me in the database: " + userName+ " Subject : "+userNameSubject);
      
          filterChain.doFilter(servletRequest, servletResponse);
      }
      

      【讨论】:

      • 谢谢!你能不能说得具体一点,我在冬天像个雪人一样出去。没有身份验证的用户转到 index.xhtml 并且在控制器类从数据库中获取一些数据后登录用户转到不同的页面。 Login--->JDBC-realm--->如果成功,控制器或其他类中的方法获取数据--->DB---->view。我应该在哪里实现该过滤器以及如何以及在哪里中断容器管理的身份验证以从经过身份验证的用户数据的数据库中获取数据?
      猜你喜欢
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多