通过过滤器Filter来完成登录访问权限限制
来源:互联网 发布:知牛财经直播 编辑:程序博客网 时间:2024/06/11 20:09
- 1.编写过滤器类
- public class MyFilter implements Filter {
- public void init(FilterConfig filterConfig) throws ServletException {
- }
- public void destroy() {
- }
- public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {
- System.out.println("该url正在过滤...");
- /**
- * 1,doFilter的第一个参数为ServletRequest对象。此对象给过滤器提供了对进入的信息(包括
- * 表单数据、cookie和HTTP请求头)的完全访问。第二个参数为ServletResponse,通常在简单的过
- * 滤器中忽略此参数。最后一个参数为FilterChain,此参数用来调用servlet或JSP页。
- * 如果处理HTTP请求,并且需要访问诸如getHeader或getCookies等在ServletRequest中
- * 无法得到的方法,就要把此request对象构造成HttpServletRequest
- */
- String currentURL = request.getRequestURI(); // 取得根目录所对应的绝对路径:
- String targetURL = currentURL.substring(currentURL.indexOf("/", 1),
- currentURL.length()); // 截取到当前文件名用于比较
- HttpSession session = request.getSession(false);
- if (!"/login.jsp".equals(targetURL)) {
- if(!"/userLogin.action".equals(targetURL)){ //不过滤登入的action
- // 判断当前页是否是重定向以后的登录页面页面,如果是就不做session的判断,防止出现死循环
- if (session == null || session.getAttribute("Competence") == null) {
- // *用户登录以后需手动添加session
- System.out.println("request.getContextPath()="
- + request.getContextPath());
- response.sendRedirect(request.getContextPath() + "/login.jsp");
- // 如果session为空表示用户没有登录就重定向到login.jsp页面
- return;
- }
- }
- }
- // 加入filter链继续向下执行
- filterChain.doFilter(request, response);
- /**
- * 调用FilterChain对象的doFilter方法。Filter接口的doFilter方法取一个FilterChain对象作 为它
- * 的一个参数。在调用此对象的doFilter方法时,激活下一个相关的过滤器。如果没有另
- * 一个过滤器与servlet或JSP页面关联,则servlet或JSP页面被激活。
- */
- }
- }
- 2.在web.xml中配置:filter元素位于部署描述符文件(web.xml)的前部,所有filter-mapping、servlet或servlet-mapping元素之前。
3.当登入成功时,在action中把用户信息写入session中。
ServletActionContext.getRequest().getSession().setAttribute("user", "username");
4.当退出账户时,在action中把session设置为null
ServletActionContext.getRequest().getSession().setAttribute("user", null);
0 0
- 通过过滤器Filter来完成登录访问权限限制
- 通过过滤器Filter来完成url访问权限限制
- 过滤器用来拦截用户是否登录访问权限的问题(通过session来进行拦截)
- jsp filter登录限制过滤器
- 利用filter()过滤器进行访问权限控制
- 使用Filter过滤器实现权限访问
- 通过session来设置登录主界面时,通过过滤器filter判断是否已经登录过,如果已经登陆过可以直接访问主界面,如果没有,需要重新登陆
- 用过滤器实现登录和访问权限
- spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制(转)
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制(转)
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Guava学习笔记:Google Guava 类库简介
- ssh框架
- gradle浅析(一)
- 建造者模式(创建型)
- Grafana简要教程
- 通过过滤器Filter来完成登录访问权限限制
- AngularJS
- 数据存储(五种方式二)-文件存储
- xcode 模拟器 home键
- Android 对话框(Dialog)大全 建立你自己的对话框
- 代码对比工具BCompare
- caffe中layer的一些特殊操作,比如split
- Java基本类型-字符类型
- 软件实施经理岗位职责