SpringBoot拦截器中返回信息提示及乱码问题

该博客介绍了如何使用Spring MVC的拦截器`AdminInterceptor`进行权限验证。拦截器检查请求上的`@UserAuth`注解,对无权限的请求返回错误信息。同时,它还涉及到缓存、菜单权限、Dozer映射等操作,确保用户只有在具备相应权限时才能访问特定资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值