基于spring boot实现归档、异常处理、登录拦截
2020-08-02 19:17
399 查看
归档功能
NewRepository
@Query("select function('date_format',n.updateTime,'%Y')as year from News n group by year order by year desc ") List<String> findGroupYear(); @Query("select n from News n where function('date_format',n.updateTime,'%Y') = ?1") List<News> findByYear(String year);
NewService
Map<String,List<News>> archiveNew(); Long countNew();
NewServiceImpl
@Override public Map<String, List<News>> archiveNew() { List<String> years = newRepository.findGroupYear(); Map<String,List<News>> map = new LinkedHashMap<>(); for (String year:years){ map.put(year,newRepository.findByYear(year)); System.out.println(year); } return map; } @Override public Long countNew() { return newRepository.count(); }
ArchiveShowController
package com.zr0726.news.web; import com.zr0726.news.service.NewService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class ArchiveShowController { @Autowired private NewService newService; @GetMapping("/archives") public String archives(Model model){ model.addAttribute("archiveMap",newService.archiveNew()); model.addAttribute("newsCount",newService.countNew()); return "archives"; } }
异常处理
handler.ControllerExceptionHandler
package com.zr0726.news.handler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; @ControllerAdvice public class ControllerExceptionHandler { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @ExceptionHandler(Exception.class) public ModelAndView exceptionHandler(HttpServletRequest request,Exception e) throws Exception{ logger.error("Request: URL: {},Exception: {}",request.getRequestURI(),e); if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){ throw e; } ModelAndView mv = new ModelAndView(); mv.addObject("url",request.getRequestURI()); mv.addObject("exception",e); mv.setViewName("error/error"); return mv; } }
NotFoundException
package com.zr0726.news; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_FOUND) public class NotFoundException extends RuntimeException{ public NotFoundException(){ } public NotFoundException(String message) { super(message); } public NotFoundException(String message, Throwable cause) { super(message, cause); } }
登录拦截
LoginInterceptor
package com.zr0726.news.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getSession().getAttribute("uer")==null){ response.sendRedirect("/admin"); return false; } return super.preHandle(request, response, handler); } }
WebConfig
package com.zr0726.news.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/admin/**") .excludePathPatterns("/admin") .excludePathPatterns("/admin/login"); } }
相关文章推荐
- [个人笔记]基于SpringBoot的新闻管理系统 归档展示/登录拦截/异常处理功能
- springboot+thymeleaf基于全局异常处理实现session过期处理
- springboot结合全局异常处理实现登录注册验证
- spring boot 默认异常处理的实现
- spring boot 使用拦截器 实现 用户登录拦截
- 基于springboot实现http响应异常信息国际化
- springboot进阶-使用aop实现接口监控和全局异常处理
- Springboot之自定义全局异常处理的实现
- 基于Spring Boot的统一异常处理设计
- 全局异常处理类的定义与错误页面实现(基于SpringBoot)
- spring boot如何基于JWT实现单点登录详解
- springboot+jwt+shiro集成实现前后端分离的登录认证和拦截
- SpringBoot中的异常处理与参数校验的方法实现
- 基于Spring Boot的统一异常处理设计
- SpringBoot整合SSM实现统一异常处理
- 轻松实现SpringBoot项目异常全局处理
- 基于CAS的单点登录SSO[5]: 基于Springboot实现CAS客户端的前后端分离
- 单点登录系统实现基于SpringBoot
- 基于SpringBoot简单实现SpringAop+Redis+cookie 单点登录 和 用户登录检测
- Spring boot项目中异常拦截设计和处理详解