Merge pull request !3 from guanshubiao/master
This commit is contained in:
随遇而安
2022-03-17 16:02:07 +00:00
committed by Gitee
2 changed files with 14 additions and 4 deletions

View File

@@ -93,7 +93,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// CSRF禁用因为不使用session
.csrf().disable()
// 认证失败处理类 TODO 启用自定义认证会导致oauth授权地址不能访问
//.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token所以不需要session
.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)
throws IOException
{
int code = HttpStatus.UNAUTHORIZED;
String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
if (isAjaxRequest(request)){
int code = HttpStatus.UNAUTHORIZED;
String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
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);
}
}