SpringBoot2.0设置拦截器
所有功能完成 配置登录认证
配置拦截器
新建包com.example.interceptor;
为企业提供网站制作、成都网站制作、网站优化、营销型网站建设、竞价托管、品牌运营等营销获客服务。成都创新互联拥有网络营销运营团队,以丰富的互联网营销经验助力企业精准获客,真正落地解决中小企业营销获客难题,做到“让获客更简单”。自创立至今,成功用技术实力解决了企业“网站建设、网络品牌塑造、网络营销”三大难题,同时降低了营销成本,提高了有效客户转化率,获得了众多企业客户的高度认可!
创建login拦截类
package com.example.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //请求进入这个拦截器 HttpSession session = request.getSession(); if(session.getAttribute("user") == null){ //判断session中有没有user信息// System.out.println("进入拦截器"); if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){ response.sendError(401); } response.sendRedirect("/"); //没有user信息的话进行路由重定向 return false; } return true; //有的话就继续操作 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
在com.example包中添加拦截控制器
package com.example;import com.example.interceptor.LoginInterceptor;import com.example.interceptor.RightsInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*;@Configuration //使用注解 实现拦截public class WebAppConfigurer implements WebMvcConfigurer { @Autowired RightsInterceptor rightsInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { //登录拦截的管理器 InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); //拦截的对象会进入这个类中进行判断 registration.addPathPatterns("/**"); //所有路径都被拦截 registration.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径 } }
在WebAppConfigurer.java中增加内容
package com.example;import com.example.interceptor.LoginInterceptor;import com.example.interceptor.RightsInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*;@Configuration //使用注解 实现拦截public class WebAppConfigurer implements WebMvcConfigurer { @Autowired RightsInterceptor rightsInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { //登录拦截的管理器 InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); //拦截的对象会进入这个类中进行判断 registration.addPathPatterns("/**"); //所有路径都被拦截 registration.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径// super.addInterceptors(registry); //权限拦截的管理器 InterceptorRegistration registration1 = registry.addInterceptor(rightsInterceptor); registration1.addPathPatterns("/**"); //所有路径都被拦截 registration1.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径 } }
当前名称:SpringBoot2.0设置拦截器
网页地址:http://scyingshan.cn/article/gidscj.html