springMVC拦截器的preHandle方法返回值是Boolean类型,所以条件够就放行,执行方法,而条件不足,前端无法知道是什么原因而被拦截,下面来解决这个问题。
一. 编写一个拦截器(该拦截器作用是:判断用户是否登录并且权限是否足够)
package com.mmall.controller.common.intercept;
import com.mmall.common.Constants;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
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;
/**
* @author Luyue
* @date 2018/8/17 18:02
**/
@Slf4j
public class AuthorityTestIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute(Constants.CURRENT_USER);
if (user == null || user.getRole().intValue() != Constants.ROLE.ROLE_ADMIN) {
return false;
}
log.info("preHandle");
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 boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute(Constants.CURRENT_USER);
if (user == null || user.getRole().intValue() != Constants.ROLE.ROLE_ADMIN) {
//重置response
response.reset();
//设置编码格式
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“application/json;charset=UTF-8”);
PrintWriter pw = response.getWriter();
if (user == null) {
pw.write(JsonUtil.objToJson(ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "拦截器拦截,请登录")));
} else {
pw.write(JsonUtil.objToJson(ServerResponse.createByErrorCodeAndMessage(ResponseCode.ERROR.getCode(), "拦截器拦截,无权限操作")));
}
pw.flush();
pw.close();
return false;
}
log.info("preHandle");
return true;
}
效果:
这篇文章没有写全面,其中pw.write(JsonUtil.objToJson(ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), “拦截器拦截,请登录”)));
这句代码中JsonUtil.objToJson这个方法没有给出代码。
不过文章的思路可以参考。
可以把缺失的部分代码换成下面的代码。
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = null ;
try{
JSONObject res = new JSONObject();
res.put("success","false");
res.put("msg","xxxx");
out = response.getWriter();
out.append(res.toString());
return false;
}
catch (Excepton e){
e.printStackTrace();
response.sendError(500);
return false;
}
以解决上面的问题。
参考文章:
https://blog.csdn.net/qq_37585236/article/details/81781563
https://blog.csdn.net/u012201619/article/details/75156707/