自学内容网 自学内容网

001、引用FeignClient循环依赖问题

1.报错信息

HandlerInterceptor引用FeignClient造成循环依赖

The dependencies of some of the beans in the application context form a cycle:

┌─────┐

| appAuthInterceptor defined in URL [jar:file:/deployments/app.jar!/BOOT-INF/classes!/com/XX/assembler/interceptor/AppAuthInterceptor.class]

↑ ↓

| hpcCommonServiceImpl (field private com.XX.assembler.client.HpcCommonFeignClient com.XX.assembler.service.impl.HpcCommonServiceImpl.hpcCommonFeignClient)

↑ ↓

| com.XX.assembler.client.HpcCommonFeignClient

↑ ↓

| org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration

↑ ↓

| interceptorConfiguration (field private com.XX.assembler.interceptor.AppAuthInterceptor com.XX.assembler.config.InterceptorConfiguration.appAuthInterceptor)

└─────┘

错误信息如下:

The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| appAuthInterceptor defined in URL [jar:file:/deployments/app.jar!/BOOT-INF/classes!/com/XX/assembler/interceptor/AppAuthInterceptor.class]
↑ ↓
| hpcCommonServiceImpl (field private com.XX.assembler.client.HpcCommonFeignClient com.XX.assembler.service.impl.HpcCommonServiceImpl.hpcCommonFeignClient)
↑ ↓
| com.XX.assembler.client.HpcCommonFeignClient
↑ ↓
| org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑ ↓
| interceptorConfiguration (field private com.XX.assembler.interceptor.AppAuthInterceptor com.XX.assembler.config.InterceptorConfiguration.appAuthInterceptor)
└─────┘

2.分析

AppAuthInterceptor

package com.XX.assembler.interceptor;

import com.alibaba.fastjson.JSON;
import com.XX.assembler.constant.Constants;
import com.XX.assembler.orm.domain.App;
import com.XX.assembler.service.AppMemberService;
import com.XX.assembler.service.AppService;
import com.XX.assembler.service.HpcCommonService;
import com.XX.assembler.util.ServletUtils;
import com.XX.techless.boot.exception.BizErrorException;
import com.XX.techless.boot.vo.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 2000
 */
@Component
public class AppAuthInterceptor implements HandlerInterceptor {
   

    @Autowired
    private AppMemberService appMemberService;

    @Autowired
    private HpcCommonService hpcCommonService;

    @Autowired
    private AppService appService;

    private static final String ERROR_MSG = "您不是应用成员或超级管理员,没有权限访问。";
    private static final String APP_ID_NOT_EXISTS = "appId不存在,请检查。";

    /**
     * appId.header.Key
     */
    @Value("${appId.header.Key:appid}")
    private String appIdHeaderKey;

    @Value("${app.id:s01974}")
    private String sCode;

    /**
     * 是否开启AppAuthInterceptor  true:开启,false:不开启
     */
    @Value("${is.open.appAuthInterceptor:false}")
    private boolean isOpenAppAuthInterceptor;

    /**
     * 前置拦截
     *
     * @param request  请求
     * @param response 响应
     * @param handler  当前对象
     * @return 是否拦截成功
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
   

        // 不开启,直接通过
        if (!isOpenAppAuthInterceptor) {
   
            return true;
        }

        String appId = request.getHeader(appIdHeaderKey);

        if (StringUtils.isBlank(appId)) {
   
            ServletUtils.renderString(response,
                    JSON.toJSONString(JsonResult.error(APP_ID_NOT_EXISTS)));
            return false;
        }

        App app = appService.selectById(appId);
        if (app == null) {
   
            throw new BizErrorException("应用不存在,请检查应用。");
        }
        // 是否是超级管理员
        boolean isAllAssetAdmin = false;
        if (StringUtils.isNotBlank(sCode)) {
   
            isAllAssetAdmin = hpcCommonService.isSuperAdmin(sCode);
        }
        if (isAllAssetAdmin) {
   
            return true;
        }

        String kCode = app.getKCode();
        if (StringUtils.isBlank(kCode)) {
   
            throw new BizErrorException("应用的kCode不能为空,请检查信息。");
        }

        // 是否是成员
        boolean isA

原文地址:https://blog.csdn.net/king27001/article/details/142494468

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!