SpringBoot学习笔记(六):配置拦截器,控制登录跳转
来源:互联网 发布:linux命令行删除文件 编辑:程序博客网 时间:2024/06/05 20:32
总共分2步:
- 配置自己的拦截器;
- 在web的配置文件中,实例化上面的拦截器,并添加规则;
拦截器代码:MyInterceptor.java
public class MyInterceptor implements HandlerInterceptor { Logger logger = LoggerFactory.getLogger(MyInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub logger.info("------preHandle------"); //获取session HttpSession session = request.getSession(true); //判断用户ID是否存在,不存在就跳转到登录界面 if(session.getAttribute("userId") == null){ logger.info("------:跳转到login页面!"); response.sendRedirect(request.getContextPath()+"/admin/login"); return false; }else{ session.setAttribute("userId", session.getAttribute("userId")); return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub }}
web配置文件:WebConfig.java
@Configuration@EnableWebMvc@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { private ApplicationContext applicationContext; public WebConfig(){ super(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/"); registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/"); super.addResourceHandlers(registry); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } @Override public void addInterceptors(InterceptorRegistry registry) { //拦截规则:除了login,其他都拦截判断 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/adminUser/login"); super.addInterceptors(registry); }}
1 0
- SpringBoot学习笔记(六):配置拦截器,控制登录跳转
- springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
- springboot学习之拦截器配置
- SpringMVC配置拦截器实现登录控制
- [学习笔记]JAVA_SSM框架简单的登录拦截器配置
- SpringBoot学习笔记之拦截器使用
- Spring中利用拦截器控制登录及页面跳转
- springboot 拦截器配置
- Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】
- Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】
- Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】
- Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】
- 添加登录拦截器【springboot mybatis学习与开发(四)】
- EJB学习笔记六(EJB中的拦截器)
- springboot学习笔记(六) Mybatis
- SpringBoot(六):过滤器和拦截器
- springboot配置拦截器,不起作用
- 配置拦截器实现对用户登录的控制
- LUA C API接口
- C# 泛型的协变和逆变
- dt cms 4.0 扩展字段 调用
- 83-非阻塞 I/O
- 提高SD读写速度的若干建议
- SpringBoot学习笔记(六):配置拦截器,控制登录跳转
- 这个MySQL入门概念总结得好
- unity中的灯光烘焙问题lightMap
- SQL将用户表中已存在的数据所有姓名(汉字)转换为拼音首字母
- [一周一译]脸书正在吞并世界
- 矩阵类
- Android 获取电池信息
- POJ2187Beauty Contest
- Android打包动态配置签名