Cấu trúc website:
- RootFolder
- Protected
- Admin
- backup.jsp
- reset.jsp
- User
- home.jsp
- order.jsp
- Admin
- Resources
- Protected
- Index.jsp
- Login.jsp
Qui định rằng chỉ tài nguyên trong thư mục Protected là cần phải chứng thực trước khi sử dụng.
Bước 1: Tạo filter đón nhận tất cả các yêu cầu truy cập đến webserver
<filter> <filter-name>Auth_Filter</filter-name> <filter-class>codes.Auth_Filter</filter-class> </filter> <filter-mapping> <filter-name>Auth_Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Bước 2: Hiệu chỉnh lại theo tác kiểm tra người dùng trong hàm doFilter, với các yêu cầu truy cập và tài nguyên cần bảo vệ thì chuyển đến trang chứng thực (login) sau đó quay lại tài nguyên mà người dùng đã yêu cầu. Trong ví dụ này chúng ta đánh dấu trạng thái đăng nhập bằng session login.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { RequestWrapper wrappedRequest = new RequestWrapper((HttpServletRequest) request); ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse) response); doBeforeProcessing(wrappedRequest, wrappedResponse); Throwable problem = null; try { HttpServletRequest req = (HttpServletRequest) wrappedRequest; HttpServletResponse res = (HttpServletResponse) wrappedResponse; HttpSession session = req.getSession(); String requestURL = req.getRequestURI(); if (session.getAttribute("login") != null || requestURL.endsWith("login.jsp") || requestURL.indexOf("/action/") &gt;= 0) { chain.doFilter(wrappedRequest, wrappedResponse); } else { if (requestURL.indexOf("/protected/") &gt;= 0) { String loginURL = req.getScheme()+"://"+ req.getServerName()+":"+req.getServerPort() +"/Lab04_Authenticate_UserRequest_TomcatUser/login.jsp"; res.sendRedirect(loginURL+"?returnURL="+requestURL); }else { chain.doFilter(wrappedRequest, wrappedResponse); } } } catch (Throwable t) { problem = t; t.printStackTrace(); } doAfterProcessing(wrappedRequest, wrappedResponse); if (problem != null) { if (problem instanceof ServletException) { throw (ServletException) problem; } if (problem instanceof IOException) { throw (IOException) problem; } sendProcessingError(problem, response); } }
Bước 3: Xây dựng trang login.jsp như sau:
Trang login nhận được thông số nơi sẽ chuyến tiếp sau khi chứng thực do filter yêu cầu.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <% String url =""; if (request.getParameter("returnURL") != null) { url = "?returnURL="+request.getParameter("returnURL"); } %> <h1>Login</h1> <form action="action/Authentication<%=url%>" method="POST"> User: <input type="text" name="txtUser" value="admin"/> User: <input type="password" name="txtPass" value="admin"/> <input type="Submit" value="Login"/> </form> </body> </html>
Nội dung Servlet Authentication như sau:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { if (request.getParameter("txtUser").equalsIgnoreCase("admin")) { // thay bằng mã nguồn chứng thực với db request.getSession().setAttribute("login", "admin"); if (request.getParameter("returnURL") != null) { response.sendRedirect(request.getParameter("returnURL") + ""); } else { response.sendRedirect("../index.jsp"); } } else //chứng thực sai sẽ làm lại { request.getRequestDispatcher("../login.jsp").forward(request, response); } } catch (Exception ex) { System.out.print(ex.toString()); out.close(); } }
Chi tiết các bước thực hiện
It’s genuinely very difficult in this active
life to listen news on Television, so I only use internet for
that reason, and obtain the most up-to-date information.
ThíchThích