From 52cd43d17ce27850986da162b610f9b2721fd30d Mon Sep 17 00:00:00 2001 From: JEECG <445654970@qq.com> Date: Wed, 13 Aug 2025 17:49:36 +0800 Subject: [PATCH] =?UTF-8?q?sas=E7=89=88=E6=9C=AC=EF=BC=8Cswagger=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=B5=8B=E8=AF=95token=E6=97=A0=E6=95=88=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=EF=BC=8C=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E8=BF=94=E5=9B=9E401=E9=94=99=E8=AF=AFjson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/common/system/util/JwtUtil.java | 1 + .../jeecg/config/security/SecurityConfig.java | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/JwtUtil.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/JwtUtil.java index d5d409e50..8ce660f36 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/JwtUtil.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/JwtUtil.java @@ -75,6 +75,7 @@ public class JwtUtil { HttpServletResponse httpServletResponse = (HttpServletResponse) response; // issues/I4YH95浏览器显示乱码问题 httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8"); + response.setContentType("application/json;charset=UTF-8"); Result jsonResult = new Result(code, errorMsg); jsonResult.setSuccess(false); OutputStream os = null; diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/security/SecurityConfig.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/security/SecurityConfig.java index 3820cb381..58af2ba22 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/security/SecurityConfig.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/security/SecurityConfig.java @@ -8,6 +8,8 @@ import com.nimbusds.jose.jwk.source.JWKSource; import com.nimbusds.jose.proc.SecurityContext; import lombok.AllArgsConstructor; import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.system.util.JwtUtil; import org.jeecg.config.security.app.AppGrantAuthenticationConvert; import org.jeecg.config.security.app.AppGrantAuthenticationProvider; import org.jeecg.config.security.password.PasswordGrantAuthenticationConvert; @@ -64,6 +66,7 @@ import java.util.stream.Collectors; @EnableWebSecurity @EnableMethodSecurity @AllArgsConstructor +@Slf4j public class SecurityConfig { private JdbcTemplate jdbcTemplate; @@ -87,14 +90,15 @@ public class SecurityConfig { .authenticationProvider(new SocialGrantAuthenticationProvider(authorizationService, tokenGenerator()))) //开启OpenID Connect 1.0(其中oidc为OpenID Connect的缩写)。 访问 /.well-known/openid-configuration即可获取认证信息 .oidc(Customizer.withDefaults()); - http - //将需要认证的请求,重定向到login页面行登录认证。 - .exceptionHandling((exceptions) -> exceptions - .defaultAuthenticationEntryPointFor( - new LoginUrlAuthenticationEntryPoint("/sys/login"), - new MediaTypeRequestMatcher(MediaType.TEXT_HTML) - ) - ); + + //将需要认证的请求,抛出异常,不跳转页面 + http.exceptionHandling(exceptions -> exceptions + .authenticationEntryPoint((request, response, authException) -> { + // 记录详细的异常信息 + log.error("接口访问失败,请求路径:{},错误信息:{}", request.getRequestURI(), authException.getMessage(), authException); + JwtUtil.responseError(response,401,authException.getMessage()); + }) + ); return http.build(); } @@ -195,6 +199,12 @@ public class SecurityConfig { return config; })) .csrf(AbstractHttpConfigurer::disable) + // 添加异常处理 + .exceptionHandling(exceptions -> exceptions + .authenticationEntryPoint((request, response, authException) -> { + log.error("接口访问失败,请求路径:{},错误信息:{}", request.getRequestURI(), authException.getMessage(), authException); + JwtUtil.responseError(response,401,authException.getMessage()); + })) .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(jeecgAuthenticationConvert))); return http.build(); }