0

I am new to spring boot microservice, was able to setup the microservice project successfully. But I tried to implement JWT on the Auth-Service which worked and

@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

enter image description here

which allows me to access the service page

enter image description here

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

@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

enter image description here

Please what is the best way to access the auth-service through the Eureka-discovery service.

Kunle Ajiboye
  • 723
  • 1
  • 8
  • 22

1 Answers1

0

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().

Bejond
  • 1,188
  • 1
  • 11
  • 18
  • thanks for the response. So from the corrections you made, I need to have JwtUser class in my Eureka proxy too, because, I already have it in my auth-service. – Kunle Ajiboye Feb 23 '18 at 08:53