符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
小编给大家分享一下javaweb设计中filter粗粒度权限控制的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
站在用户的角度思考问题,与客户深入沟通,找到天全网站设计与天全网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、域名与空间、虚拟主机、企业邮箱。业务覆盖天全地区。1 说明
我们给出三个页面:index.jsp、user.jsp、admin.jsp。
index.jsp:谁都可以访问,没有限制;
user.jsp:只有登录用户才能访问;
admin.jsp:只有管理员才能访问。
2 分析
设计User类:username、password、grade,其中grade表示用户等级,1表示普通用户,2表示管理员用户。
当用户登录成功后,把user保存到session中。
创建LoginFilter,它有两种过滤方式:
如果访问的是user.jsp,查看session中是否存在user;
如果访问的是admin.jsp,查看session中是否存在user,并且user的grade等于2。
3 代码
LoginServlet com.cug.web.servlet.LoginServlet LoginServlet /LoginServlet index.jsp UserFilter com.cug.filter.UserFilter UserFilter /user/* AdminFilter com.cug.filter.AdminFilter AdminFilter /admin/*
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 Mapusers = new HashMap (); 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+"/"; %>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > My JSP 'admin.jsp' starting page admin.jsp
${user.username }
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用户页
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >系统管理员
<%@ 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+"/"; %>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > My JSP 'user.jsp' starting page user.jsp
${user.username }
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用户登陆界面
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >管理员登陆界面
用户登录
<%@ 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+"/"; %>" method="post"> 用户名:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > My JSP 'login.jsp' starting page ${msg }
<%@ 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+"/"; %>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > My JSP 'index.jsp' starting page index.jsp
${user.username }
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >用户登陆界面
" rel="external nofollow" rel="external nofollow" rel="external nofollow" >管理员登陆界面
看完了这篇文章,相信你对“javaweb设计中filter粗粒度权限控制的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!