1、新建一个登陆检查的拦截器
必须实现HandlerInterceptor这个接口,重写其中的三个方法
**登录检查
* 1、配置好拦截器要拦截哪些请求
* 2、把这些配置放在容器中
* @author shkstart
* @create 2021-05-11 19:37
* @
*/
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
/**
*目标方法执行之前
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
log.info("preHandle拦截的请求路径是{}",requestURI);
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null){
return true;
}
//拦截住。未登录。跳转到登录页
request.setAttribute("msg", "请先登录");
// response.sendRedirect("/");
request.getRequestDispatcher("/").forward(request, response);
return false;
}
/**
* 目标方法执行以后
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 页面渲染之后
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
2、配置拦截器
/**
* 1、编写一个拦截器实现HandlerInterceptor接口
* 2、拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)
* 3、指定拦截规则【如果是拦截所有,静态资源也会被拦截】
*/
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") //所有请求都被拦截包括静态资源
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); //放行的请求
}
}
addPathPatterns("/") //所有请求都被拦截包括静态资源
excludePathPatterns("/","/login","/css/","/fonts/","/images/","/js/**"); //放行的请求
文件上传及下载
- 导入表单html页面
- 添加名称空间xmlns:th="http://www.w3.org/1999/xhtml别忘了
文件上传下载
可以选择多文件 上传
文件上传表单固定写法 /**
* MultipartFile 自动封装上传过来的文件
* @param email
* @param username
*
* @param photos
* @return
*/
@PostMapping("/upload")
public String upload(@RequestParam("email") String email,
@RequestParam("username") String username,
@RequestPart("headImage") MultipartFile headerImg,
@RequestPart("photos") MultipartFile[] photos) throws IOException {
log.info("上传的信息email={},username={},headerImg={},photos={}",
email,username,headerImg.getSize(),photos.length);
String storePath = "C:\\Users\\admin\\Desktop\\abc";
if(!headerImg.isEmpty()){
//保存到服务器
String headerImgFileName = headerImg.getOriginalFilename();
headerImg.transferTo(new File(storePath + File.separator + headerImgFileName));
}
if(photos.length>0){
for (MultipartFile photo : photos) {
if(!photo.isEmpty()){
String photoFileName = photo.getOriginalFilename();
photo.transferTo(new File(storePath + File.separator + photoFileName));
}
}
}
return "main";
}
文件上传在MultipartAutoConfiguration
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
设置单个文件最大为不超过10MB,总大小为不超过100MB