[英]How to communicate with jwt enable service in microservice
I am new to spring boot microservice, was able to setup the microservice project successfully. 我是Spring Boot微服务的新手,能够成功设置微服务项目。 But I tried to implement JWT on the Auth-Service which worked and
但是我试图在Auth-Service上实现JWT,
@RestController
@RequestMapping("/token")
public class JwtController {
@Autowired
private JwtTokenGenerator jwtGenerator;
//constructor
public JwtController(JwtTokenGenerator jwtGenerator) {
this.jwtGenerator = jwtGenerator;
}
@PostMapping
public String generate(@RequestBody final JwtUser jwtUser) {
return jwtGenerator.generate(jwtUser);
}
}
was able to call it from Postman 能够从邮递员那里打电话
which allows me to access the service page 这使我可以访问服务页面
but when I tried to access the same service 但是当我尝试访问相同的服务时
@RestController
@RequestMapping("/secure/logon")
public class LogOnController {
@GetMapping
public String logOn() {
return "You are logon successfully";
}
}
from the Eureka discovery service through the Eureka Server 从Eureka发现服务通过Eureka服务器
@SuppressWarnings("deprecation")
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "fallback", groupKey = "Hello",
commandKey = "hello",
threadPoolKey = "helloThread")
@PostMapping(value = "/index")
public String token() {
String url = "http://user-service/token";
return restTemplate.getForObject(url, String.class);
}
@GetMapping(value = "/logon")
public String index() {
String url = "http://user-service/secure/logon";
return restTemplate.getForObject(url, String.class);
}
public String fallback(Throwable hystrixCommand) {
return "User-Service Failed ";
}
}
I get error 我得到错误
Please what is the best way to access the auth-service through the Eureka-discovery service. 请问通过Eureka发现服务访问身份验证服务的最佳方法是什么。
Maybe your code 也许你的代码
@PostMapping(value = "/index")
public String token() {
String url = "http://user-service/token";
return restTemplate.getForObject(url, String.class);
}
should change to 应该更改为
@PostMapping(value = "/index")
public String token(@RequestBody final JwtUser jwtUser) {
String url = "http://user-service/token";
return restTemplate.postForObject(url, jwtUser, String.class);
}
Because the service /token is a PostRequest, so you should use postForObject(). 因为服务/ token是PostRequest,所以您应该使用postForObject()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.