spring-boot 拦截器 HandlerInterceptor

继承这个类  HandlerInterceptor 


package com.jeeplus.modules.utils;


import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.capacity.poweruser.entity.PowerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class AuthorityInterceptor extends ParentController implements HandlerInterceptor {

@Resource(name="redisTemplate2")
RedisTemplate redisTemplate;

//preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();

LoginRequired annotation = method.getAnnotation(LoginRequired.class); //只拦截带有@LoginRequired的接口
if (annotation!=null){
System.out.println("开始拦截");
String token = request.getParameter("token");
String online = getOnline(token); //是否在线
String port = getPort(token); //哪一端登录
if (online.equals("1") && StringUtils.isNotBlank(port)){
return false;
}
}

//单端登录
Login annot = method.getAnnotation(Login.class); //只拦截带有@Login的接口
if (annot != null) {
System.out.println("开始拦截");
String token = request.getParameter("token");
String idCard = getIdCard(token); //身份证号
String redisToken = (String) redisTemplate.boundValueOps(idCard).get();
if (redisToken==null) {

} else if (!token.equals(redisToken)) {
System.out.println("账号重复登录了");
response.sendError(400,"您的账号已在另一台设备登录,请勿重复登录");
return false;
}
//延长redis中token的时长
redisTemplate.boundValueOps(idCard).set(token,10, TimeUnit.MINUTES);
}

return true;
}

//这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。
//postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之后,也就是在Controller的方法调用之后执行,
// 但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操作。
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {

}

//该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。
//该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行。
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {

}


}
 

自定义注解如下,根据自己的项目需求 可用可不用

package com.jeeplus.modules.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {
}
package com.jeeplus.modules.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义注解
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Login {
}

 

posted @ 2022-03-09 15:32  知行IT讲堂  阅读(214)  评论(0编辑  收藏  举报