如何实现
首先拦截器是属于web这块的,那我们需要引入springboot web模块,具体版本在parent中
org.springframework.boot spring-boot-starter-web
然后我们在config目录下新建interceptor目录,用来放拦截器
- 我们新建两个拦截器,一个用来登陆拦截,一个用来进行管理员拦截。
- 登陆拦截不必多说,就是判断除登陆请求外所有请求是否登陆过,没有我们就截断请求,然会返回失败状态
- 管理员拦截,拦截的是管理员才能访问的资源,比如用户管理/权限管理等,它是登陆拦截放行后才能进行的,
- 就像关卡是一关一关的。登陆验证后进入管理员验证,判断该登陆用户是否具有管理员权限,若拥有则放行,否则拦截并返回失败状态
- 拦截器的核心是实现 org.springframework.web.servlet.HandlerInterceptor 接口,验证逻辑一般写在 preHandle 方法中,在请求资源前
- 我的验证逻辑比较简单,登陆验证就是从session中取出用户信息,判断是否存在,用户信息是登陆成功时放入session中的,大家也可以使用JWT验证
- 管理员验证也是从session中取出管理员信息,判断是否是管理员,管理员信息是登陆成功时将是否是管理员的信息放入session中的,大家也可以使用JWT验证
登陆拦截器
package com.example.interceptor_demo.config.interceptor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; /** * 拦截器,登录检查 */ @Component public class LoginInterceptor implements HandlerInterceptor { @Autowired private HttpSession session; @Autowired private ObjectMapper objectMapper; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object user = session.getAttribute("sessionUser"); if (sessionUser!=null){ return true; }else { MapnotLogin = new HashMap<>(); notLogin.put("msg","not login"); notLogin.put("code",403); notLogin.put("data",null); try(PrintWriter printWriter = response.getWriter()){ printWriter.print(objectMapper.writeValueAsString(notLogin)); }catch (Exception e){ e.printStackTrace(); } return false; } } @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 { } }
管理员拦截器
package com.example.interceptor_demo.config.interceptor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; /** * 拦截器,管理员验证 */ @Component public class AdminInterceptor implements HandlerInterceptor { @Autowired private HttpSession session; @Autowired private ObjectMapper objectMapper; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Boolean isAdmin = (Boolean)session.getAttribute("sessionAdmin"); if (isAdmin!=null && isAdmin){ return true; }else { MapnotLogin = new HashMap<>(); notLogin.put("msg","no power"); notLogin.put("code",403); notLogin.put("data",null); try(PrintWriter printWriter = response.getWriter()){ printWriter.print(objectMapper.writeValueAsString(notLogin)); }catch (Exception e){ e.printStackTrace(); } return false; } } @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 { } }
最后我们在config目录下新建WebMvcConfig类,用来注册拦截器
- 拦截器注册类的核心是实现 org.springframework.web.servlet.config.annotation.WebMvcConfigurer 接口
- 实现 addInterceptors 方法,参数 registry 对象可用来注册拦截器
- registry.addInterceptor() 方法用来添加拦截器
- .addPathPatterns() 方法是为该拦截器添加拦截资源路径
- .excludePathPatterns() 方法是为该拦截器添加要放行的资源路径
- 其中 * 代表路径下任意名称,** 代表任意路径下任意名称
package com.example.interceptor_demo.config; import com.example.interceptor_demo.config.interceptor.AdminInterceptor; import com.example.interceptor_demo.config.interceptor.LoginInterceptor; import org.springframework.beans.factory.annotation.Autowired; 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 WebMvcConfig implements WebMvcConfigurer { @Autowired private LoginInterceptor loginInterceptor; @Autowired private AdminInterceptor adminInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { this.loginInterceptor(registry);//登录拦截 this.adminInterceptor(registry);//管理员拦截 } private void loginInterceptor(InterceptorRegistry registry){ registry.addInterceptor(loginInterceptor) .addPathPatterns("/**") .excludePathPatterns(//释放登陆接口 "/login/**" ); } private void adminInterceptor(InterceptorRegistry registry){ registry.addInterceptor(htmlPageInterceptor) .addPathPatterns("/admin/**");//拦截管理员接口 } }
到此这篇关于Java SpringBoot 使用拦截器作为权限控制的文章就介绍到这了,更多相关Java SpringBoot拦截器权限控制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!