修改bug-添加认证失败处理类 导致启用自定义认证会导致oauth授权地址不能访问

This commit is contained in:
guanshubiao
2022-03-17 23:33:48 +08:00
parent ef8e262d08
commit 7d06b42ea6
2 changed files with 14 additions and 4 deletions

View File

@@ -93,7 +93,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// CSRF禁用因为不使用session // CSRF禁用因为不使用session
.csrf().disable() .csrf().disable()
// 认证失败处理类 TODO 启用自定义认证会导致oauth授权地址不能访问 // 认证失败处理类 TODO 启用自定义认证会导致oauth授权地址不能访问
//.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token所以不需要session // 基于token所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// 过滤请求 // 过滤请求

View File

@@ -27,8 +27,18 @@ public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, S
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
throws IOException throws IOException
{ {
if (isAjaxRequest(request)){
int code = HttpStatus.UNAUTHORIZED; int code = HttpStatus.UNAUTHORIZED;
String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI()); String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg))); ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
}else {
response.sendRedirect("/oauth/login");
}
}
public static boolean isAjaxRequest(HttpServletRequest request) {
String ajaxFlag = request.getHeader("X-Requested-With");
return ajaxFlag != null && "XMLHttpRequest".equals(ajaxFlag);
} }
} }