wisdomisite-java/src/main/java/com/zhgd/xmgl/security/RestAuthenticationAccessDeniedHandler.java
2023-02-16 15:28:15 +08:00

40 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.security;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @program: wisdomSite
* @description: 权限不足处理类返回403
* @author: Mr.Peng
* @create: 2021-03-05 16:23
**/
@Component("RestAuthenticationAccessDeniedHandler")
@Slf4j
public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
//登陆状态下,权限不足执行该方法
log.info("权限不足:" + e.getMessage());
response.setStatus(403);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter printWriter = response.getWriter();
JSONObject resParam = new JSONObject();
resParam.put("message", e.getMessage());
resParam.put("success", false);
resParam.put("code", "403");
printWriter.write(resParam.toJSONString());
printWriter.flush();
}
}