博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc之拦截器实现用户登录权限校验
阅读量:3906 次
发布时间:2019-05-23

本文共 6310 字,大约阅读时间需要 21 分钟。

文章目录

前言

很多时候,都可以用拦截器来实现用户登录的权限的判断:当用户进行登录的时候,如果想要直接进入一个网站的首页面,那么点击对应的前端页面的主页面按钮之后,那么就需要在后台进行权限检验,判断用户是不是已经登录过,也就是说session中有没有存放数据,如果没有存放数据,那么就需要跳转到登录页面,反之则直接进入首页,而这就可以通过拦截器来处理,下面我们进入springmvc拦截器的使用步骤吧!!!


一、springmvc拦截器是什么?

拦截器是springmvc定义的一个功能,它跟原生的servlet中的filter过滤器有同样的功能,不过它使用起来更加方便, 首先客户端发起请求进入DispatcherServlet,再进入我们自定义的拦截器,拦截器实现的是HandlerIntercepter接口,如果拦截成功.

就向客服端返回拦截器响应信息。
如果验证通过,就进入对应的controller,然后结果一系列操作,返回controller响应信息。

二、使用步骤

1.导入相关的依赖

pom.xml:

代码如下(示例):

4.0.0
org.example
spring_intercepter
1.0-SNAPSHOT
junit
junit
4.12
org.springframework
spring-webmvc
5.1.9.RELEASE
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2

3.web.xml文件

springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springmvc
/
charsetfilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
charsetfilter
/*

4.springmvc核心配置文件

5.controller层

package com.lhh.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;@Controller@RequestMapping("/user")public class UserControlelr {
/*用户登录,传递参数,调用该方法*/ @RequestMapping("/login") public String login( String username, String password, HttpServletRequest request, Model model){
System.out.println("88888888888888"); HttpSession session = request.getSession(); session.setAttribute("userLoginInfo","xxx"); model.addAttribute("username",username); return "main"; } /*用户点击登录,请求传过来,调用该方法,跳到login页面*/ @RequestMapping("/goLogin") public String goLogin(){
return "login"; } @RequestMapping("/main") public String goMain(){
return "main"; } @RequestMapping("/logout") public String logout(HttpSession session){
//因为要退出登录,所以需要将session中的数据清除 session.removeAttribute("userLoginInfo"); return "login"; }}

6.拦截器定义

package com.lhh.config;import org.springframework.web.servlet.AsyncHandlerInterceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class UserInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(); System.out.println("request.getRequestURI()="+request.getRequestURI()); //在index.jsp页面点击首页,然后因为没有用户的登录信息,所以需要跳转到登录页面,(此时也是第一次登录,那么应该放行,不然输入了用户名和密码之后还是不会登进去 // 因为session并没有存放数据,会走下面的请求转发,服务器内部再次跳到登录页面) if(request.getRequestURI().contains("login")){
return true; } /*如果session中带有参数,那么已经登录过,直接放行,可以进首页*/ if (session.getAttribute("userLoginInfo") != null) {
return true; } /*直接请求登录,用户直接点击了登录按钮放行*/ if (request.getRequestURI().contains("goLogin")) {
return true; } //请求转发跳到登录页面,因为不能直接登录,属于第一次登录。 request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
} public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}}

登录页面login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>    登录
用户名:
密码:

7.前端代码

主页面main.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>    Title    

首页

${
username}

注销用户

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>      $Title$        

登录

首页

配置tomcat服务器:

在这里插入图片描述
在这里插入图片描述
贴一下项目结构:
在这里插入图片描述


运行截图,点击登录: ![在这里插入图片描述](https://img-blog.csdnimg.cn/2020100810184524.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNDg2Nzc1,size_16,color_FFFFFF,t_70#pic_center)

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
再次回到index.jsp点击首页,那么就可以直接登录了,因为之前已经用“的士速递”进行登录过了。
在这里插入图片描述

总结

写一个拦截器,对请求进行拦截,需要让自己的类实现springmvc中的HandlerInterceptor接口,然后实现接口中的方法,一般来说,只需要写preHandle()方法即可,(而postHandle()和afterHandle()方法两个想法用于跟日志相关的操作,后续应该会使用到。)就可以实现在请求发送,调用相关的方法的时候,先进行拦截,处理,处理完毕之后,根据处理的结果判断放行还是不放行。

另外,还有写一个细节,那就是,每次导完依赖的时候,需要在file–>project-structure---->artifacts中把lib文件添加到WEB-INF下,并把pom.xml中的依赖都导入到lib文件夹下(没有lib文件夹需要手动创建)。

转载地址:http://lrlen.baihongyu.com/

你可能感兴趣的文章
Redis + Django Session Cookie
查看>>
REST and RPC
查看>>
Web Architecture Basic idea
查看>>
Session & Cookie
查看>>
count lines in a file - wc & nl
查看>>
View the start/end of a file linux
查看>>
General overview of the Linux file system
查看>>
/proc
查看>>
Nginx TCP Load Balance
查看>>
需要注意的食物
查看>>
Nginx upstream schedule strategy
查看>>
Redis Brief Intro
查看>>
Nginx Basic Config
查看>>
Nginx Load Balancer Config
查看>>
Nginx config hight throughput
查看>>
mysql max_connection config
查看>>
Python improve performance
查看>>
mysql interview questions and answers
查看>>
File & File system size limitation for Redhat
查看>>
Python decorator guide
查看>>