in.hocg.sso.server.sample.config.security.user.AjaxAccessDeniedHandler Maven / Gradle / Ivy
The newest version!
package in.hocg.sso.server.sample.config.security.user;
import cn.hutool.json.JSONUtil;
import in.hocg.boot.utils.struct.result.ExceptionResult;
import in.hocg.boot.utils.struct.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by hocgin on 2020/1/9.
* email: [email protected]
* 登录后,访问被拒绝
*
* @author hocgin
*/
@Slf4j
public class AjaxAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.warn("登录后,访问被拒绝", accessDeniedException);
ExceptionResult result = ExceptionResult.fail(HttpServletResponse.SC_UNAUTHORIZED, ResultCode.ACCESS_DENIED_ERROR.getMessage());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
try (final PrintWriter writer = response.getWriter()) {
writer.write(JSONUtil.toJsonStr(result));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy