diff --git a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/sentinel/handle/GlobalBizExceptionHandler.java b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/sentinel/handle/GlobalBizExceptionHandler.java index 0b37f152..1547eae2 100644 --- a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/sentinel/handle/GlobalBizExceptionHandler.java +++ b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/sentinel/handle/GlobalBizExceptionHandler.java @@ -85,11 +85,11 @@ public class GlobalBizExceptionHandler { */ @ExceptionHandler(AccessDeniedException.class) @ResponseStatus(HttpStatus.FORBIDDEN) - public R handleAccessDeniedException(AccessDeniedException e) { + public void handleAccessDeniedException(AccessDeniedException e) { String msg = SpringSecurityMessageSource.getAccessor().getMessage("AbstractAccessDecisionManager.accessDenied", e.getMessage()); log.error("拒绝授权异常信息 ex={}", msg, e); - return R.failed(e.getLocalizedMessage()); + throw new RuntimeException(msg); } /** diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java deleted file mode 100755 index 9da6850d..00000000 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pig4cloud.pig.common.security.component; - -/** - * @author lengleng - * @date 2019/2/1 - */ - -import cn.hutool.http.HttpStatus; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.pig4cloud.pig.common.core.constant.CommonConstants; -import com.pig4cloud.pig.common.core.exception.PigDeniedException; -import com.pig4cloud.pig.common.core.util.R; -import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.PrintWriter; - -/** - * @author lengleng 授权拒绝处理器,覆盖默认的OAuth2AccessDeniedHandler 包装失败信息到PigDeniedException - */ -@Slf4j -@RequiredArgsConstructor -public class PigAccessDeniedHandler extends OAuth2AccessDeniedHandler { - - private final ObjectMapper objectMapper; - - /** - * 授权拒绝处理,使用R包装 - * @param request request - * @param response response - * @param authException authException - */ - @Override - @SneakyThrows - public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) { - log.info("授权失败,禁止访问 {}", request.getRequestURI()); - response.setCharacterEncoding(CommonConstants.UTF8); - response.setContentType(CommonConstants.CONTENT_TYPE); - R result = R.failed(new PigDeniedException("授权失败,禁止访问")); - response.setStatus(HttpStatus.HTTP_FORBIDDEN); - PrintWriter printWriter = response.getWriter(); - printWriter.append(objectMapper.writeValueAsString(result)); - } - -} diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerAutoConfiguration.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerAutoConfiguration.java index 3328b74d..dda4ad36 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerAutoConfiguration.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerAutoConfiguration.java @@ -35,10 +35,6 @@ public class PigResourceServerAutoConfiguration { return new PermissionService(); } - @Bean - public PigAccessDeniedHandler pigAccessDeniedHandler(ObjectMapper objectMapper) { - return new PigAccessDeniedHandler(objectMapper); - } @Bean public PigBearerTokenExtractor pigBearerTokenExtractor(PermitAllUrlProperties urlProperties) {