代码拉取完成,页面将自动刷新
package com.strong.config.security.point;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.strong.utils.mvc.pojo.view.ReplyEnum;
import com.strong.utils.mvc.pojo.view.ReplyVO;
import com.strong.utils.security.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;
import static com.strong.config.WebSecurityConfig.STR_URL_LOGIN_URL;
/**
* JWT认证入口点
*
* @author Simen
* @date 2022/02/10
*/
@Slf4j
@Component("JwtAuthenticationEntryPoint")
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
// 从request头中获取Accept
String strAccept = request.getHeader("Accept");
if (StrUtil.isNotBlank(strAccept)) {
// 对Accept分组为字符串数组
String[] strsAccept = StrUtil.splitToArray(strAccept, ",");
// 判断Accept数组中是否存在"text/html"
if (ArrayUtil.contains(strsAccept, "text/html")) {
// 存在"text/html",判断为html访问,则跳转到登录界面
response.sendRedirect(STR_URL_LOGIN_URL);
} else {
// 不存在"text/html",判断为json访问,则返回未授权的json
SecurityUtils.returnReplyJsonResponse(response, HttpServletResponse.SC_OK,
new ReplyVO<>(ReplyEnum.ERROR_TOKEN_EXPIRED));
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。