关于feign:springCould整合feign出现required-a-bean-of-type-xxx-not-be-found

158次阅读

共计 3125 个字符,预计需要花费 8 分钟才能阅读完成。

一、问题

springCould 整合 feign 提醒required a bean of type xxx that could not be found

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-05-23 11:51:02.777 ERROR 17160 --- [main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field oAuth2FeignClient in com.quantsmart.service.impl.SysLoginServiceImpl required a bean of type 'com.quantsmart.feign.OAuth2FeignClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.quantsmart.feign.OAuth2FeignClient' in your configuration.


Process finished with exit code 1

com/quantsmart/feign/OAuth2FeignClient.java

package com.quantsmart.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: kaiyi
 * @Date 2021/5/22 16:14
 */

@FeignClient(value = "authorization-server")
public interface OAuth2FeignClient {@PostMapping("/oauth/token")
    ResponseEntity<JwtToken> getToken(@RequestParam("grant_type") String grantType , // 受权类型
                                      @RequestParam("username") String username , // 用户名
                                      @RequestParam("password") String  password , // 用户的明码
                                      @RequestParam("login_type")  String loginType,  // 登录的类型
                                      @RequestHeader("Authorization") String basicToken // Basic Y29pbi1hcGk6Y29pbi1zZWNyZXQ= 由第三方客户端信息加密呈现的值
                                      );
    ;


}

com/quantsmart/service/impl/SysLoginServiceImpl.java

package com.quantsmart.service.impl;



/**
 * @author: kaiyi
 * @Date 2021/5/22 15:55
 */
@Service
@Slf4j
public class SysLoginServiceImpl implements SysLoginService {

    @Autowired
    private OAuth2FeignClient oAuth2FeignClient;

    @Autowired
    private SysMenuService sysMenuService;

    @Value("${basic.token:Basic Y29pbi1hcGk6Y29pbi1zZWNyZXQ=}")
    private String basicToken ;

    /**
     * 登录的实现
     *
     * @param username 用户名
     * @param password 用户的明码
     * @return 登录的后果
     */
    @Override
    public LoginResult login(String username, String password) {log.info("用户 {} 开始登录", username);

        // 1、获取 Token 须要近程调用 authorization-server 服务
        ResponseEntity<JwtToken> tokenResponseEntity = oAuth2FeignClient.getToken("password", username, password,
                "admin_type", basicToken);

        if(tokenResponseEntity.getStatusCode() != HttpStatus.OK){throw new ApiException(ApiErrorCode.FAILED);
        }

        JwtToken jwtToken = tokenResponseEntity.getBody();

        log.info("近程调用受权服务器胜利, 获取的 token 为{}", JSON.toJSONString(jwtToken,true));
        String token = jwtToken.getAccessToken();

        // 2、查问咱们的菜单
        Jwt jwt = JwtHelper.decode(token);
        String jwtClaimsStr = jwt.getClaims();
        JSONObject jwtJson = JSON.parseObject(jwtClaimsStr);
        Long userId = Long.valueOf(jwtJson.getString("user_name")) ;
        List<SysMenu> menus = sysMenuService.getMenusByUserId(userId);

        // 3 权限数据怎么查问 -- 不须要查问的,因为咱们的 jwt 外面曾经蕴含了
        JSONArray authoritiesJsonArray = jwtJson.getJSONArray("authorities");
        List<SimpleGrantedAuthority> authorities = authoritiesJsonArray.stream() // 组装咱们的权限数据
                .map(authorityJson->new SimpleGrantedAuthority(authorityJson.toString()))
                .collect(Collectors.toList());
        return new LoginResult(token, menus, authorities);

    }
}

解决方案

https://blog.csdn.net/huangwe…

原来是启动类忘了加注解了:

@EnableFeignClients

正文完
 0