redstar/redstar-cloud/redstar-cloud-common/src/main/java/business/common/handler/RedStarAuthExceptionEntryPo...

41 lines
1.6 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 business.common.handler;
import business.common.utils.ResponseTool;
import business.common.vo.result.ResponseCode;
import business.common.vo.result.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author charry 用来解决匿名用户访问无权限资源时的异常
*/
@Slf4j
@Component
public class RedStarAuthExceptionEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
int status = HttpServletResponse.SC_OK;
String message = "token不合法";
if (StringUtils.containsIgnoreCase(message, "Invalid access token")) {
message = "访问令牌不正确";
}
if (StringUtils.containsIgnoreCase(message, "Full authentication is required to access this resource")) {
message = "请求头client信息不正确烦请核对";
status = HttpServletResponse.SC_OK;
}
log.error(message, authException);
ResponseVo vo = new ResponseVo(ResponseCode.SYSTEM_LOGIN_EXPIRE_EXCEPTION.getCode(), message);
ResponseTool.makeResponse(response, MediaType.APPLICATION_JSON_VALUE, status, vo);
}
}