springCould整合feign提示required a bean of type xxx that could not be found
java
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
web
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
spring
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); // 一、獲取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(); // 二、查詢咱們的菜單 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...服務器
原來是啓動類忘了加註解了:app
@EnableFeignClients