springmvc拦截器登录验证示例

网友投稿 205 2023-06-07


springmvc拦截器登录验证示例

一开始,学了拦截器与过滤器,咋一看两者有点像,实际上两者有很大的不同。就用拦截器和过滤器分别做了登录验证试验,这次先说拦截器。下面是自己实践的一个实例:

在spring-mvc.xml中配置拦截器:

如上所示,这里配置了LoginIntercepter,为了简单起见,该过滤器只拦截了URL为"/user/*"的请求。

要拦截的请求对应控制器如下:

import java.util.ArrayList;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowirehttp://d;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.wyb.domain.User;

import com.wyb.service.IUserService;

import com.wyb.service.impl.UserServiceImpl;

@Controller

@RequestMapping("/user")

public class UserController {

   private static final Logger LOG=Logger.getLogger(UserController.class);

   @Autowired

   private IUserService userService;

@RequestMapping("/showAllUser")

public String showAllUser(Model m){

List userlhttp://ist=new ArrayList();

userlist=userService.findAllUser();

for(User user :userlist){

System.out.println(user.getUserName());

}

return "/jsp/showAllUser";

}

}

这里的showAllUser()方法是为了输出所有的用户,为了表明执行了方法,将所有用户在后台打印,URL为:http://localhost:8080/TestSSM/user/showAllUser,可见该URL肯定会被LoginIntercepter拦截。

测试页面showAllUser.jsp如下:

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

pageEncoding="UTF-8"%>

show All User

this is showAllUser Page!!!

LoginIntercepter如下:

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import com.wyb.domain.User;

public class LoginInterceptor implements HandlerInterceptor{

@Override

public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println("this is afterCompletion of LoginInterceptor");

}

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)

throws Exception {

System.out.println("this is postHandle of LoginInterceptor");

}

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

// TODO Auto-generated method stub

System.out.println("this is preHandle of LoginInterceptor");

HttpSession session=request.getSession();

User user=(User)session.getAttribute("user");

if(user==null){

System.out.println("no user in LoginInterceptor!!!");

request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);

}

//返回true代表继续往下执行

return true;

}

}

这里我犯了一个错误,聪明的小伙伴也许已经看出来了,如果按照上面的代码,当我们访问:http://localhost:8080/TestSSM/user/showAllUser结果如下:

咋一看,成功拦截了,输入用户名信息,正常跳转到主页,再次进入http://localhost:8080/TestSSM/user/showAllUser如下:

页面正常输出,已经记录了session,不会被再次拦截,看似成功了,可是看看后台输出:

有没有发现,我们执行了两次showAllUser()方法,可见第一次访问虽然被拦截器拦截了下来进入登录页面,但后台已经悄悄执行了showAllUser()。为什么呢?我们回头再看看LoginIntercepter.java,尤其是preHandle()方法:

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

// TODO Auto-generated method stub

System.out.println("this is preHandle of LoginInterceptor");

HttpSession session=request.getSession();

User user=(User)session.getAttribute("user");

if(user==null){

System.out.println("no user in LoginInterceptor!!!");

request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);

}

//返回true代表继续往下执行

return true;

}

在判断user为空后,虽然执行了页面跳转,但是程序还是会继续执行,最后返回true,返回true意味着,被拦截的业务逻辑可以继续往下执行,因此,虽然表面上被拦截了,但从本质上来说并没有拦截到。因此需要修改如下:

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

// TODO Auto-generated method stub

System.out.println("this is preHandle of LoginInterceptor");

HttpSession session=request.getSession();

User user=(User)session.getAttribute("user");

if(user==null){

System.out.println("no user in LoginInterceptor!!!");

request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);

//本次访问被拦截,业务逻辑不继续执行

return false;

}

//返回true代表继续往下执行

return true;

}

user为空,跳转后,返回false,就不会执行被拦截的业务逻辑了,修改后后台输出如下:

现在后台正常输出,且session保存了user信息后,才能执行showAllUser()方法,大功告成!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈regExp的test方法取得的值变化的原因及处理方法
下一篇:微信小程序 使用腾讯地图SDK详解及实现步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~