One, the problem
springCould integrate feign prompt 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);
}
}
solution
https://blog.csdn.net/huangweijian93/article/details/109334010
It turns out that the startup class forgot to add annotations:
@EnableFeignClients
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。