栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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.*;


@Configuration
@Slf4j
@AllArgsConstructor
public class AdminInterceptor implements HandlerInterceptor  {

    private AuthService authService;

    private SysMenuDao sysMenuDao;

    private RedisUtils redisUtils;

    public AdminInterceptor() {

    }

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

    }

    
    @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 {

    }





    public static  List mapList(Collection sourceList, Class destinationClass) {
        DozerBeanMapper dozer = new DozerBeanMapper();
        List destinationList = new ArrayList<>();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号