shiro通过认证webapp上的角色重定向

时间:2014-11-07 06:43:10

标签: java security login jax-rs shiro

我们正在使用Shiro作为Sequrity使用JAX-RS(Jersey)编写JAVA WebApp。

我们有:

  • 3个不同的角色,每个角色都有自己的主页
  • 所有用户的单一登录页面

当用户登录时,如何在身份验证后根据他的角色将用户重定向到他自己的主页

来自shiro ini的一些代码:

authc.loginUrl = /login.html

[urls]
/apps/admin/** = authc, roles["admin"]
/apps/teacher/** = authc, roles[teacher]
/apps/student/** = authc, roles[student]

例如:

  1. 有人试图访问我们的网络应用,他将被重定向到 login.html
  2. 输入用户名/密码后,他将根据角色重定向到自己的主页
    • /apps/admin/home.html 如果他有 admin 角色
    • /apps/teacher/home.html 如果他有老师角色
    • /apps/student/home.html 如果他有学生角色

1 个答案:

答案 0 :(得分:1)

我有同样的要求,我按照以下方式做了......

 if (currentUser.isAuthenticated()) {  
      if (currentUser.hasRole("admin")) {
          response.sendRedirect("app/admin/adminpage.jsp");
      }
      else if (currentUser.hasRole("student") || currentUser.hasRole("admin")) {           
         response.sendRedirect("app/student/studentpage.jsp");         
      }
     else if (currentUser.hasRole("teacher") || currentUser.hasRole("admin")) {      
         response.sendRedirect("app/teacher/teacherpage.jsp");
     }
  } 
 else {
         response.sendRedirect("app/login.jsp");
   }