Servlet身份驗證過濾器
可以在過濾器中執行身份認證。 在這裏,我們將如何在過濾器類檢查由用戶提交的密碼,如果給定密碼爲admin
,它將把請求轉發到WelcomeAdmin
servlet,否則會顯示錯誤消息。
使用過濾器驗證用戶的示例
下面來看看如何使用過濾器驗證用戶的簡單示例。
在這個示例中創建了以下幾個主要的代碼文件:
- index.html - 首頁
- MyFilter.java - 過濾器,用於處理用戶登錄信息和跳轉。
- AdminServlet.java - 管理員的Servlet
- web.xml - 項目描述符和配置信息。
打開Eclipse,創建一個動態Web項目:AuthenticationFilter,其完整的目錄結構如下所示 -
以下是這個項目中的幾個主要的代碼文件。
文件:index.html -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet身份驗證過濾器</title>
</head>
<body>
<div style="font-align: center;">
<form action="login" method="post">
用戶名:<input type="text" name="username" value="maxsu"/>密碼:<input type="password"
name="password" /><input type="submit" value="登錄">
</form>
</div>
</body>
</html>
文件:MyFilter.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter {
public void init(FilterConfig arg0) throws ServletException {
//
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String password = request.getParameter("password");
if (password == null) {
password = "";
}
if (password.equals("admin")) {
chain.doFilter(request, response);// sends request to next resource
} else {
out.print("用戶名或密碼錯誤!");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.include(request, response);
}
}
public void destroy() {
System.out.println("MyFilter.destroy()");
}
}
文件:AdminServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class AdminServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("歡迎您來到 ADMIN頁面~");
out.close();
}
}
文件:web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>AuthenticationFilter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>com.yiibai.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>com.yiibai.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/login</url-pattern>
</filter-mapping>
</web-app>
在編寫上面代碼後,部署此Web應用程序(在項目名稱上點擊右鍵->」Run On Server…」),打開瀏覽器訪問URL: http://localhost:8080/SimpleFilter/ ,如果沒有錯誤,應該會看到以下結果 -
輸入密碼:admin,然後提交表單登錄,應該會看到以下結果 -