本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
博文前提:
在上篇文章Spring Boot项目实践-员工管理系统(四)·登录功能实现里面完成了登录功能,但是此时用户可以不登陆便可进入主页,这是不安全的,为了解决这个问题,便可使用拦截器。
一、新建登录拦截器LoginHandlerIntercepter.java
package com.example.employee_management.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @className: LoginHandlerIntercepter
* @description: 登录拦截器
*/
public class LoginHandlerIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取登录用户的session
Object loginUser=request.getSession().getAttribute("loginUser");
//session不存在,即用户尚未登录
if(loginUser==null){
request.setAttribute("msg","请先登录");
//返回首页
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
return true;
}
}
}
二、在MVC配置类中添加登录拦截器
MyMvcConfig.java
package com.example.employee_management.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @className: MyMvcConfig
* @description: MVC 控制器 借助注解完成控制器而不用手动编写
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
/**
* MVC 添加首页控制器
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//这里 “/”和“/index.html”效果一样,因为web项目默认页是index.html
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
//添加用户主页
registry.addViewController("/main.html").setViewName("dashboard");
}
/**
* 将国际化组件放入ioc容器中
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
/**
* 添加登录拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerIntercepter()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**");
}
}
三、修改登录控制器:添加session来保证拦截器正常运行
LoginController.java
package com.example.employee_management.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
/**
* @className: LoginController
* @description: 登录控制器
*/
@Controller
public class LoginController {
/**
* 登录
* @param username
* @param password
* @param model
* @return
*/
@RequestMapping("/user/login")
public String login(@RequestParam("username")String username,
@RequestParam("password")String password, Model model,
HttpSession session){
//用户名不为空且密码正确 注意这里的数据是默认密码为123456,便于调试
if(!StringUtils.isEmpty(username) && "123456".equals(password)){
session.setAttribute("loginUser",username);
return "redirect:/main.html";
}else {
//用户名或密码错误
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
启动项目后,在浏览器中输入
localhost:8080/main.html
便会转入到首页(index.html),并且提示用户尚未登录。
登录拦截器的功能便已完成。
2020.04.12