【问题标题】:How to restrict every user roles in JSP/Servlet?如何限制 JSP/Servlet 中的每个用户角色?
【发布时间】:2013-01-27 06:18:58
【问题描述】:

我想在我的 JPS Web 应用程序中提供许多用户。我不想为每个用户重定向很多页面。我只想为所有用户提供一页。例如,我有一个页面包含添加、编辑和删除按钮,这是管理员用户的主要或唯一角色。如果登录用户不是管理员,我不希望任何用户有权添加、编辑和删除。

【问题讨论】:

    标签: java jsp jakarta-ee servlets


    【解决方案1】:

    即使您为不同的角色使用相同的 JSP 页面也是可能的。 JSP 在服务器中编译并转换为原始 HTML 和 js,然后发送给客户端。

    所以在 JSP 页面中你可以放置用户角色的条件基础。喜欢-

    LoginServlet -

    public class LonginServelt extends HttpServlet{
        public void doPost(HttpServletRequest request, HttpServletResponse response){
            User user = userService.checkUserCredential(username,password);
            Session session = request.getSession();
            session.setAttribute("user",user);
        }
    }
    
    <c:choose>
      <c:when test="${isAdmin}">
        You got Gold 
      </c:when>
    
      <c:when test="${isCustomer}">
        You got Silver 
      </c:when>
    
      <c:when test="${isProducer}">
        You got Bronze 
      </c:when>
    
      <c:otherwise>
        Better luck next time 
      </c:otherwise>
    </c:choose>
    

    因此,当用户在服务器本身中使用不同角色访问此页面时,它将填充角色依赖的 html。

    注意:您甚至可以使用 scriplet 来放置被视为 旧技术。

    【讨论】:

    • 但是如何在登录时设置用户角色并在我的jsp页面中调用值?
    • 您必须在登录时验证用户凭据以及您应该保存到 server-session(request.getSession(false)) 中的用户 dto 或模型。您将从会话中获取 jsp 页面中的用户角色。
    • 很抱歉,但我发现自己更难理解这部分,因为我对这部分真的很陌生......你能为我提供一个示例解决方案吗?谢谢 :)
    • 检查Example
    • 我尝试使用上面的示例...我已将 jstl jar 文件添加到我的构建路径中,但我仍然有此错误 rg.apache.jasper.JasperException: /WEB-INF/jsp/home .jsp(第 1 行,第 1 列)绝对 uri:java.sun.com/jsp/jstl/core 无法在 web.xml 或随此应用程序部署的 jar 文件中解析
    【解决方案2】:

    你想要的是一个过滤器,确切地说是一个会话过滤器,你可以试试这些:

    如果没有,我假设你有一个用户类:

    用户.java

    public class User implements Serializable {
      private int accountId;
      private String loginId;
      private Role type;
    
      public User(int accountId, String loginId, Role type) {
        this.accountId = accountId;
        this.loginId = loginId;
        this.type = type;
      }
    
      public User() {
        this.accountId = -1;
        this.loginId = null;
        this.type = null;
      }
    
      public void setRole(Role type) {
        this.type = type;
      }
    
      public Role getRole() {
        return this.type;
      }
    
      public void setAccountId(int accountId) {
        this.accountId = accountId;
      }
    
      public int getAccountId() {
        return this.accountId;
      }
    
      public void setLoginId(String loginId) {
        this.loginId = loginId;
      }
    
      public String getLoginId() {
        return this.loginId;
      }
    }
    

    您还可以为您的角色类型创建一个枚举:

    角色.java

    public enum Role {
    
      ADMINISTRATOR, STAFF;
    }
    

    在您的 login.jsp 中,这只是给您一个想法的示例:

    <%
      //put your login query stuff here
      User user = new User();
      user.setAccountId(1);
      user.setLoginId("adminaccount01);
      user.setRole(Role.ADMINISTRATOR);
      session.setAttribute("LOGIN_USER", user);
    %>
    

    这里是过滤器:SessionCheckFilter.java

    public class SessionCheckFilter implements Filter {
    
        private String contextPath;
    
        @Override
        public void init(FilterConfig fc) throws ServletException {
            contextPath = fc.getServletContext().getContextPath();
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
    
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;                        
    
            User user = (User) req.getSession().getAttribute("LOGIN_USER");
            if (user == null) {                
                    //put your redirect stuff here
                    res.sendRedirect(contextPath + "/to_your_login.jsp");                
            } else {
                switch (user.getRole()) {
                    case ADMINISTRATOR:
                            //put your redirect stuff here
                            res.sendRedirect(contextPath + "/redirect_to_your_admin_path/admin_page.jsp");
                        break;
                    case STAFF:
                            //put your redirect stuff here
                            res.sendRedirect(contextPath + "/redirect_to_staff_path/staff_page.jsp");
                        break;
                    default:
                        break;
                }
                fc.doFilter(request, response);
            }
        }
    
        @Override
        public void destroy() {
        }
    }
    

    并添加不要忘记将这些添加到 web.xml

      <filter>
        <filter-name>SessionCheckFilter</filter-name>
        <filter-class>package_name_if_there_is_any.SessionCheckFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>SessionCheckFilter</filter-name>
        <url-pattern>/your_path/*</url-pattern> 
      </filter-mapping>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2019-06-17
      • 2016-12-23
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多