PeterJohn
PeterJohn

Reputation: 589

How to restrict every user roles in JSP/Servlet?

I want to provide many users in my JPS web application. I dont want to have many pages to be redirected for every user. I only want one page for all the user. For example, I have one page that contains add, edit and delete button which is the primary or only role of the admin users. If the login user is not admin I dont want any user to have access for add, edit and delete.

Upvotes: 1

Views: 8832

Answers (2)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41210

It is possible even if you are using same JSP page for different role. JSP compiled in server and transformed into raw HTML & js before sending it to client.

So in JSP page you can put condition basis of user role. like -

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>

So when user hit this page with different role in server itself it will populate role depended html.

Note : you can even use scriplet to put condition which is treated as old technology.

Upvotes: 3

Joseph Caracuel
Joseph Caracuel

Reputation: 974

what you want is a filter a sessionfilter to be precise, you can try these:

i assume you have a user class if not:

User.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;
  }
}

you can also create an enum for your role types:

Role.java

public enum Role {

  ADMINISTRATOR, STAFF;
}

in your login.jsp, this is just an example to give you an idea:

<%
  //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);
%>

here is the filter: 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() {
    }
}

and add don't forget to add these to 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>

Upvotes: 1

Related Questions