最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例

 更新時(shí)間:2017年10月14日 14:10:42   作者:bluzelee2011  
這篇文章主要介紹了javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例,小編覺得還是挺不錯的,需要的朋友可以參考。

1 說明

我們給出三個(gè)頁面:index.jsp、user.jsp、admin.jsp。

index.jsp:誰都可以訪問,沒有限制;

user.jsp:只有登錄用戶才能訪問;

admin.jsp:只有管理員才能訪問。

2 分析

設(shè)計(jì)User類:username、password、grade,其中g(shù)rade表示用戶等級,1表示普通用戶,2表示管理員用戶。

當(dāng)用戶登錄成功后,把user保存到session中。

創(chuàng)建LoginFilter,它有兩種過濾方式:

如果訪問的是user.jsp,查看session中是否存在user;
如果訪問的是admin.jsp,查看session中是否存在user,并且user的grade等于2。

3 代碼

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<servlet> 
 <servlet-name>LoginServlet</servlet-name> 
 <servlet-class>com.cug.web.servlet.LoginServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
 <servlet-name>LoginServlet</servlet-name> 
 <url-pattern>/LoginServlet</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
 <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
<filter> 
 <filter-name>UserFilter</filter-name> 
 <filter-class>com.cug.filter.UserFilter</filter-class> 
</filter> 
<filter-mapping> 
 <filter-name>UserFilter</filter-name> 
 <url-pattern>/user/*</url-pattern> 
</filter-mapping> 
<filter> 
 <filter-name>AdminFilter</filter-name> 
 <filter-class>com.cug.filter.AdminFilter</filter-class> 
</filter> 
<filter-mapping> 
 <filter-name>AdminFilter</filter-name> 
 <url-pattern>/admin/*</url-pattern> 
</filter-mapping> 
</web-app> 

LoginServlet.java

package com.cug.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cug.domain.User;
import com.cug.web.service.UserService;
public class LoginServlet extends HttpServlet{
	@Override 
	 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
	   throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		User user = UserService.login(username, password);
		if(user == null){
			req.setAttribute("msg", "用戶名或者密碼錯誤");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		} else{
			req.getSession().setAttribute("user", user);
			req.getRequestDispatcher("index.jsp").forward(req,resp);
		}
	}
}

UserService

package com.cug.web.service;
import java.util.HashMap;
import java.util.Map;
import com.cug.domain.User;
public class UserService {
	private static Map<String, User> users = new HashMap<String, User>();
	static{
		users.put("zhu", new User("zhu", "123", 2));
		users.put("xiao", new User("xiao", "123", 1));
	}
	public static User login(String username, String password){
		User user = users.get(username);
		if(user == null) 
		   return null;
		if(!user.getPassword().equals(password)) 
		   return null;
		return user;
	}
}

AdminFilter

package com.cug.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.cug.domain.User;
public class AdminFilter implements Filter{
	@Override 
	 public void destroy() {
	}
	@Override 
	 public void doFilter(ServletRequest req, ServletResponse resp, 
	   FilterChain chain) throws IOException, ServletException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		HttpServletRequest request = (HttpServletRequest)req;
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			resp.getWriter().print("用戶還沒有登陸");
			request.getRequestDispatcher("/login.jsp").forward(req, resp);
		}
		if(user.getGrade() < 2){
			resp.getWriter().print("您的等級不夠");
			return;
		}
		chain.doFilter(req, resp);
	}
	@Override 
	 public void init(FilterConfig arg0) throws ServletException {
	}
}

UserFilter

package com.cug.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.cug.domain.User;
public class UserFilter implements Filter{
	@Override 
	 public void destroy() {
	}
	@Override 
	 public void doFilter(ServletRequest request, ServletResponse response, 
	   FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpServletRequest httpReq = (HttpServletRequest)request;
		User user = (User)httpReq.getSession().getAttribute("user");
		if(user == null){
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		chain.doFilter(request, response);
	}
	@Override 
	 public void init(FilterConfig filterConfig) throws ServletException {
	}
}

User

package com.cug.domain;
public class User {
	private String username;
	private String password;
	private int grade;
	public User() {
		super();
	}
	public User(String username, String password, int grade) {
		super();
		this.username = username;
		this.password = password;
		this.grade = grade;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	@Override 
	 public String toString() {
		return "User [username=" + username + ", password=" + password 
		    + ", grade=" + grade + "]";
	}
}

html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 <title>My JSP 'admin.jsp' starting page</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 --> 
 </head> 
 <body> 
 <h1>admin.jsp</h1> 
 <h3>${user.username }</h3> 
 <a href="<c:url value='/index.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a><br/> 
 <a href="<c:url value='/user/user.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用戶頁</a><br/> 
 <a href="<c:url value='/admin/admin.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >系統(tǒng)管理員</a><br/> 
 </body> 
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 <title>My JSP 'user.jsp' starting page</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 --> 
 </head> 
 <body> 
 <h1>user.jsp</h1> 
 <h3>${user.username }</h3> 
 <a href="<c:url value='/index.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a><br> 
 <a href="<c:url value='/user/user.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用戶登陸界面</a><br> 
 <a href="<c:url value='/admin/admin.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >管理員登陸界面</a><br> 
 </body> 
</html> 

用戶登錄

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 <title>My JSP 'login.jsp' starting page</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 --> 
 </head> 
 <body> 
 ${msg } 
 <form action="<c:url value='/LoginServlet'/>" method="post"> 
  用戶名:<input type="text" name="username"/><br/> 
  密碼:<input type="password" name="password"/><br/> 
  <input type="submit" value="登陸"/> 
 </form> 
 </body> 
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 <title>My JSP 'index.jsp' starting page</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
 --> 
 </head> 
 <body> 
 <h1>index.jsp</h1> 
 <h3>${user.username }</h3> 
 <a href="<c:url value='/index.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a><br> 
 <a href="<c:url value='/user/user.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用戶登陸界面</a><br> 
 <a href="<c:url value='/admin/admin.jsp'/>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >管理員登陸界面</a><br> 
 </body> 
</html> 

總結(jié)

以上就是本文關(guān)于javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例的全部內(nèi)容,感興趣的朋友可以繼續(xù)參閱:JavaWeb項(xiàng)目中dll文件動態(tài)加載方法解析(詳細(xì)步驟)、Javaweb使用cors完成跨域ajax數(shù)據(jù)交互Javaweb項(xiàng)目session超時(shí)解決方案等。

希望對大家有所幫助,如有不足之處,歡迎留言指正。感謝大家對本站的支持!

相關(guān)文章

  • Java單例模式的講解

    Java單例模式的講解

    今天小編就為大家分享一篇關(guān)于Java單例模式的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 使用Zxing實(shí)現(xiàn)二維碼生成器內(nèi)嵌圖片

    使用Zxing實(shí)現(xiàn)二維碼生成器內(nèi)嵌圖片

    二維碼在現(xiàn)實(shí)中的應(yīng)用已經(jīng)很廣泛了,本文介紹了使用Zxing實(shí)現(xiàn)二維碼生成器內(nèi)嵌圖片,有需要的可以了解一下。
    2016-10-10
  • Java對比兩個(gè)實(shí)體的差異分析

    Java對比兩個(gè)實(shí)體的差異分析

    這篇文章主要介紹了Java對比兩個(gè)實(shí)體的差異分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java 最重要布局管理器GridBagLayout的使用方法

    Java 最重要布局管理器GridBagLayout的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很復(fù)雜的布局,可以說GridBagLayout是必須要學(xué)好的的,需要的朋友可以了解下
    2012-12-12
  • springboot之如何獲取項(xiàng)目目錄路徑

    springboot之如何獲取項(xiàng)目目錄路徑

    這篇文章主要介紹了springboot之如何獲取項(xiàng)目目錄路徑問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢

    springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢

    這篇文章主要介紹了springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring配置shiro時(shí)自定義Realm中屬性無法使用注解注入的解決辦法

    Spring配置shiro時(shí)自定義Realm中屬性無法使用注解注入的解決辦法

    今天小編就為大家分享一篇關(guān)于Spring配置shiro時(shí)自定義Realm中屬性無法使用注解注入的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié)

    關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié)

    這篇文章主要介紹了關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java基礎(chǔ)教程之封裝與接口

    Java基礎(chǔ)教程之封裝與接口

    這篇文章主要介紹了Java基礎(chǔ)教程之封裝與接口,本文用淺顯易懂的語言講解了Java中的封裝與接口,很形象的說明了這兩個(gè)面向?qū)ο笮g(shù)語,需要的朋友可以參考下
    2014-08-08
  • 詳解Java中如何使用日志庫在代碼中添加日志

    詳解Java中如何使用日志庫在代碼中添加日志

    這篇文章主要為大家介紹了Java中如何使用日志庫在代碼中添加日志詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論

紫金县| 武隆县| 东港市| 巴东县| 许昌市| 大悟县| 乡宁县| 德安县| 舞钢市| 车致| 洞头县| 澄城县| 四子王旗| 两当县| 通榆县| 苏州市| 南宫市| 丰台区| 广宗县| 安宁市| 平谷区| 江川县| 玛多县| 华容县| 呈贡县| 石台县| 浑源县| 蒲江县| 石阡县| 津市市| 寿阳县| 疏勒县| 犍为县| 冕宁县| 建昌县| 黄陵县| 安远县| 勐海县| 克什克腾旗| 工布江达县| 富民县|