Java SpringMVC

1、自定义拦截器实现HandlerInterceptor接口

  1. package com.fcant.ssmcom.interceptor;
  2. import com.fcant.ssmcom.bean.User;
  3. import org.springframework.web.servlet.HandlerInterceptor;
  4. import org.springframework.web.servlet.ModelAndView;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. /**
  9. * LoginHandlerIntercepter
  10. * <p>
  11. * encoding:UTF-8
  12. *
  13. * @author Fcscanf
  14. * @description
  15. * @date 下午 17:01 2019-08-07/0007
  16. */
  17. public class LoginHandlerInterceptor implements HandlerInterceptor {
  18. /**
  19. * 配置不要拦截的URL
  20. */
  21. private final String[] URI = {"/page/login", "/user/login"};
  22. @Override
  23. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  24. StringBuffer requestURL = request.getRequestURL();
  25. //URL:除了login.jsp是可以公开访问的,其余的URL都要进行拦截控制
  26. if (requestURL.indexOf(URI[0]) > 0 || requestURL.indexOf(URI[1]) > 0) {
  27. return true;
  28. }
  29. //获取session
  30. HttpSession session = request.getSession();
  31. User user = (User) session.getAttribute("user");
  32. //判断session里是不是有登录信息
  33. if (user != null) {
  34. return true;
  35. }
  36. request.setAttribute("msg", "你还没有登录,请先登录!");
  37. request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
  38. return false;
  39. }
  40. @Override
  41. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  42. }
  43. @Override
  44. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  45. }
  46. }

2、编写Controller在Session保存用户信息

  1. package com.fcant.ssmcom.controller;
  2. import com.fcant.ssmcom.bean.User;
  3. import com.fcant.ssmcom.service.UserService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.servlet.ModelAndView;
  10. import javax.servlet.http.HttpSession;
  11. /**
  12. * UserController
  13. * <p>
  14. * encoding:UTF-8
  15. *
  16. * @author Fcscanf
  17. * @description
  18. * @date 下午 19:35 2019-08-06/0006
  19. */
  20. @Controller
  21. @RequestMapping("/user")
  22. @Slf4j
  23. public class UserController {
  24. @Autowired
  25. UserService userService;
  26. @RequestMapping("/login")
  27. public ModelAndView regUser(User user, HttpSession session) {
  28. ModelAndView modelAndView = new ModelAndView();
  29. if (user.getNumber() == null || "".equals(user.getNumber())) {
  30. modelAndView.addObject("msg", "请输入正确的工号/学号!");
  31. modelAndView.setViewName("login");
  32. return modelAndView;
  33. } else {
  34. // 查询符合条件的用户-检查用户的存在性
  35. User selectByNumber = userService.selectByNumber(user);
  36. if (selectByNumber == null) {
  37. modelAndView.addObject("msg", "未找到符合条件的用户,请输入正确的工号/学号!");
  38. modelAndView.setViewName("login");
  39. return modelAndView;
  40. }else {
  41. // 校验用户的密码信息
  42. if (selectByNumber.getPassword().equals(user.getPassword())) {
  43. // 用户名密码正确则根据用户类型跳转至不同的管理页面
  44. // 将用户信息保存至Session中
  45. session.setAttribute("user", selectByNumber);
  46. switch (selectByNumber.getrId()) {
  47. case 1:
  48. log.info("Student Login :" + selectByNumber);
  49. modelAndView.setViewName("student");
  50. return modelAndView;
  51. case 2:
  52. log.info("Teacher Login :" + selectByNumber);
  53. modelAndView.setViewName("teacher");
  54. return modelAndView;
  55. default :
  56. log.info("Admin Login :" + selectByNumber);
  57. modelAndView.setViewName("admin");
  58. return modelAndView;
  59. }
  60. }else {
  61. modelAndView.addObject("msg", "请输入正确的密码!");
  62. modelAndView.setViewName("login");
  63. return modelAndView;
  64. }
  65. }
  66. }
  67. }
  68. @RequestMapping(value = "/signOut")
  69. public String signOut(HttpSession session, Model model) {
  70. model.addAttribute("msg", "您已退出登录!");
  71. session.removeAttribute("user");
  72. return "login";
  73. }
  74. }

3、在spring-mvc.xml配置拦截器

  1. <!-- 配置登陆拦截器 -->
  2. <mvc:interceptors>
  3. <mvc:interceptor>
  4. <mvc:mapping path="/**"/>
  5. <bean class="com.fcant.ssmcom.interceptor.LoginHandlerInterceptor" />
  6. </mvc:interceptor>
  7. </mvc:interceptors>