package cn.service.auth;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson2.JSON;
import cn.common.enums.ResultCode;
import cn.common.page.response.Result;
import cn.util.core.ServletKit;
import cn.util.string.StringKit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**权限不足处理器
 * @Description: 权限拒绝处理逻辑
 */
@Component
public class UserAccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
    	/**
		 *  返回给浏览器一个400错误的HTTP页面；*/
		int code = ResultCode.HTTP_UNAUTHORIZED.getCode();
		String msg = StringKit.format("请求访问：{}，权限拒绝，无法访问系统资源", request.getRequestURI());
		ServletKit.renderString(response, JSON.toJSONString(Result.error(code, msg)));
    }
}
