1:在config层中新建LoginHandlerInterceptor类,使得这个类继承HandlerInterceptor并重写方法
2:在controller层中写可以传入用户session的方法,首先添加HttpSession在具体业务前面传入session的值![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/a0c31493b20e31e6d2fa5e97b65435e3.png)
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/6ea2635c6ed4f1bb871a50ff0bdff54b.png)
3:在LoginHandlerInterceptor类中编写代码:
package com.example.springbootweb.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author ${范涛之}
* @Description
* @create 2021-09-19 16:16
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登陆成功之后,应该有用户的session
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser==null ){ //没有登陆
request.setAttribute("msg","没有权限,请先登录");
request.getRequestDispatcher("./index.html").forward(request,response);
return false;
}else {
return true;
}
}
}
4:编写LoginController类
package com.example.springbootweb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
/**
* @author ${范涛之}
* @Description
* @create 2021-09-19 14:00
*/
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(
@RequestParam("username") String username,
@RequestParam("password") String password,
Model model, HttpSession session){
//具体业务
if (!StringUtils.isEmpty(username) && "123456".equals(password)){
session.setAttribute("loginUser",username);
return "redirect:/main.html";
}
//告诉用户登陆失败了
else {
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
5:在MyMvcConfig中配置需要拦截的东西
package com.example.springbootweb.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;
/**
* @author ${范涛之}
* @Description
* @create 2021-09-19 10:16
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
// 添加视图控制
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//自定义的国际化就生效了
@Bean
public LocaleResolver localeResolver(){ return new MyLocalResolver(); }
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/*","/js/**","/img/**");
}
}