1.在springBoot下通过使用拦截器完成在没有登陆的前提下,不允许访问其他资源
编写拦截器,要实现HandlerInterceptor
@Component
public class UserInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
//这里的User是登陆时放入session的
Users user = (Users) session.getAttribute("user");
if (user == null){
request.getRequestDispatcher("/login.jsp").forward(request, response);
return false;
}else {
return true; //如果session里有user,表示该用户已经登陆,放行,用户即可继续调用自己需要的接口
}
}
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{
}
}
2.在和启动类同一层编写配置类同时可以设置默认的欢迎页面
@SpringBootConfiguration
public class SpringMvcConfigure implements WebMvcConfigurer
{
@Autowired
public UserInterceptor userInterceptor;
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
}
<你是我自罚三杯也不肯开口的秘密>