1

JPSWebアプリケーションで多くのユーザーを提供したいと思います。すべてのユーザーにリダイレクトされる多くのページを持ちたくありません。すべてのユーザーに1ページだけが必要です。たとえば、管理者ユーザーの主要な役割または唯一の役割である追加、編集、および削除ボタンを含む1つのページがあります。ログインユーザーが管理者でない場合、追加、編集、削除のアクセス権をどのユーザーにも持たせたくありません。

4

2 に答える 2

3

同じ JSP ページを別のロールで使用していても可能です。JSP はサーバーでコンパイルされ、生の HTML および js に変換されてからクライアントに送信されます。

したがって、JSPページでは、ユーザーロールの条件ベースを配置できます。お気に入り -

ログインサーブレット -

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 を使用して、古いテクノロジとして扱われる条件を設定することもできます。

于 2013-01-27T06:26:32.813 に答える
1

あなたが望むのは、正確にはセッションフィルターのフィルターです。これらを試すことができます:

そうでない場合は、ユーザークラスがあると思います:

ユーザー.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;
  }
}

ロール タイプの列挙型を作成することもできます。

Role.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>
于 2013-01-27T07:28:20.013 に答える