package cn.wanda.common.Interceptor;
import cn.wanda.common.annotation.UserAuth;
import cn.wanda.common.base.PerAuthorityEntity;
import cn.wanda.common.utils.RedisUtils;
import cn.wanda.modules.sso.service.impl.AuthService;
import cn.wanda.modules.sso.utils.AuthConstant;
import cn.wanda.modules.sys.dao.SysMenuDao;
import cn.wanda.modules.sys.entity.SysMenuEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dozer.DozerBeanMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.*;
/**
* 拦截器 通过@UserAuth 实现拦截
*
* @author : WangYuanYi
* @description :
* @create :2021-11-22 09:03:00
*/
@Configuration
@Slf4j
@AllArgsConstructor
public class AdminInterceptor implements HandlerInterceptor {
private AuthService authService;
private SysMenuDao sysMenuDao;
private RedisUtils redisUtils;
public AdminInterceptor() {
}
/**
* 目标方法执行之前
*
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!StringUtils.isEmpty(s2) && !authUrlHashSet.contains(s2)) {
// 解决乱码问题 必须写在response.getWriter() 之前
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
JSONObject res = new JSONObject();
res.put("msg", "暂无权限");
res.put("code", "500");
out = response.getWriter();
out.append(res.toString());
return false;
} catch (Exception e) {
e.printStackTrace();
response.sendError(500);
return false;
}
}
return true;
}
/**
* 目标方法执行完成之后
*
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 页面渲染以后
*
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
DozerBeanMapper dozer = new DozerBeanMapper();
List<T> destinationList = new ArrayList<>();
for (Object sourceObject : sourceList) {
T destinationObject = dozer.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}
}
SpringBoot拦截器中返回信息提示及乱码问题
最新推荐文章于 2025-03-11 09:42:19 发布