diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index ada966ca..b1fef096 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -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() // 过滤请求 diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/security/handle/AuthenticationEntryPointImpl.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/security/handle/AuthenticationEntryPointImpl.java index c22dd324..6a934c77 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/security/handle/AuthenticationEntryPointImpl.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/security/handle/AuthenticationEntryPointImpl.java @@ -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); } }