Mukit09
Mukit09

Reputation: 3399

API calling from Postman is working, but API calling from another service using Retrofit is not working

I have built a service (Service A). Suppose I am calling an API to get token. This is working fine for postman. This is my response then:

enter image description here

Then log of Service A is given below:

2021-02-16 23:03:04:311 [http-nio-127.0.0.1-8083-exec-6] DEBUG web.FilterChainProxy:208 - Securing POST /token/get-token
2021-02-16 23:03:04:311 [http-nio-127.0.0.1-8083-exec-6] DEBUG context.SecurityContextPersistenceFilter:102 - Set SecurityContextHolder to empty SecurityContext
2021-02-16 23:03:04:311 [http-nio-127.0.0.1-8083-exec-6] DEBUG annotation.RequestMappingHandlerMapping:510 - Mapped to com.a.b.c.d.security.controller.api.JwtAuthenticationController#getToken(TokenRequest)
2021-02-16 23:03:04:312 [http-nio-127.0.0.1-8083-exec-6] WARN  filter.JwtAuthenticationFilter:59 - Authorization Header missing or not formed correctly
2021-02-16 23:03:04:312 [http-nio-127.0.0.1-8083-exec-6] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:03:04:312 [http-nio-127.0.0.1-8083-exec-6] DEBUG authentication.AnonymousAuthenticationFilter:96 - Set SecurityContextHolder to anonymous SecurityContext
2021-02-16 23:03:04:312 [http-nio-127.0.0.1-8083-exec-6] DEBUG intercept.FilterSecurityInterceptor:210 - Authorized filter invocation [POST /token/get-token] with attributes [permitAll]
2021-02-16 23:03:04:312 [http-nio-127.0.0.1-8083-exec-6] DEBUG web.FilterChainProxy:323 - Secured POST /token/get-token
2021-02-16 23:03:04:313 [http-nio-127.0.0.1-8083-exec-6] DEBUG servlet.DispatcherServlet:91 - POST "/token/get-token", parameters={}
2021-02-16 23:03:04:313 [http-nio-127.0.0.1-8083-exec-6] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:03:04:313 [http-nio-127.0.0.1-8083-exec-6] DEBUG annotation.RequestMappingHandlerMapping:510 - Mapped to com.robi.redcube.retailer.commission.security.controller.api.JwtAuthenticationController#getToken(TokenRequest)
2021-02-16 23:03:04:313 [http-nio-127.0.0.1-8083-exec-6] DEBUG annotation.RequestResponseBodyMethodProcessor:91 - Read "application/json;charset=UTF-8" to [com.robi.redcube.retailer.commission.dto.request.TokenRequest@5a1a32d7]
2021-02-16 23:03:04:314 [http-nio-127.0.0.1-8083-exec-6] DEBUG api.JwtAuthenticationController:36 - generating token
2021-02-16 23:03:04:315 [http-nio-127.0.0.1-8083-exec-6] DEBUG utility.JwtTokenUtil:52 - Token found: eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJDT01NSVNTSU9OX1NFUlZJQ0UiLCJleHAiOjE2MTM2NzQ5ODQsImlhdCI6MTYxMzQ5NDk4NCwidXNlcm5hbWUiOiJ0ZXN0X3VzZXIifQ.PxlhQbwxfRGOlGdxm7Om3drJVY2hcvFrVwj63Z85m1UYvWP3UGRrDHspL8Fe1y8ZjX7gFmqPNJ84reY3PgEkOA
2021-02-16 23:03:04:315 [http-nio-127.0.0.1-8083-exec-6] DEBUG annotation.HttpEntityMethodProcessor:255 - Using 'application/json', given [*/*] and supported [application/json]
2021-02-16 23:03:04:316 [http-nio-127.0.0.1-8083-exec-6] DEBUG annotation.HttpEntityMethodProcessor:91 - Writing [com.a.b.c.d.dto.response.util.ResponseBody@54ddc7e4]
2021-02-16 23:03:04:317 [http-nio-127.0.0.1-8083-exec-6] DEBUG servlet.DispatcherServlet:1131 - Completed 200 OK
2021-02-16 23:03:04:317 [http-nio-127.0.0.1-8083-exec-6] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:03:04:317 [http-nio-127.0.0.1-8083-exec-6] DEBUG context.SecurityContextPersistenceFilter:118 - Cleared SecurityContextHolder to complete request

But when calling the same API from service B, I am getting 404 in Service A. Then log of Service A is:

2021-02-16 23:00:56:079 [http-nio-127.0.0.1-8083-exec-3] DEBUG web.FilterChainProxy:208 - Securing POST /token/generate-token
2021-02-16 23:00:56:079 [http-nio-127.0.0.1-8083-exec-3] DEBUG context.SecurityContextPersistenceFilter:102 - Set SecurityContextHolder to empty SecurityContext
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] WARN  filter.JwtAuthenticationFilter:59 - Authorization Header missing or not formed correctly
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] DEBUG authentication.AnonymousAuthenticationFilter:96 - Set SecurityContextHolder to anonymous SecurityContext
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] DEBUG intercept.FilterSecurityInterceptor:210 - Authorized filter invocation [POST /token/generate-token] with attributes [permitAll]
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] DEBUG web.FilterChainProxy:323 - Secured POST /token/generate-token
2021-02-16 23:00:56:080 [http-nio-127.0.0.1-8083-exec-3] DEBUG servlet.DispatcherServlet:91 - POST "/token/generate-token", parameters={}
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] WARN  servlet.PageNotFound:1271 - No mapping for POST /token/generate-token
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] DEBUG servlet.DispatcherServlet:1131 - Completed 404 NOT_FOUND
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] DEBUG context.SecurityContextPersistenceFilter:118 - Cleared SecurityContextHolder to complete request
2021-02-16 23:00:56:081 [http-nio-127.0.0.1-8083-exec-3] DEBUG web.FilterChainProxy:208 - Securing POST /error
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG context.SecurityContextPersistenceFilter:102 - Set SecurityContextHolder to empty SecurityContext
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG authentication.AnonymousAuthenticationFilter:96 - Set SecurityContextHolder to anonymous SecurityContext
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG web.FilterChainProxy:323 - Secured POST /error
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG servlet.DispatcherServlet:91 - "ERROR" dispatch for POST "/error", parameters={}
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG SessionRepositoryFilter.SESSION_LOGGER:304 - No session found by id: Caching result for getSession(false) for this HttpServletRequest.
2021-02-16 23:00:56:082 [http-nio-127.0.0.1-8083-exec-3] DEBUG annotation.RequestMappingHandlerMapping:510 - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2021-02-16 23:00:56:083 [http-nio-127.0.0.1-8083-exec-3] DEBUG annotation.HttpEntityMethodProcessor:255 - Using 'application/json', given [*/*] and supported [application/json, application/*+json]
2021-02-16 23:00:56:083 [http-nio-127.0.0.1-8083-exec-3] DEBUG annotation.HttpEntityMethodProcessor:91 - Writing [{timestamp=Tue Feb 16 23:00:56 BDT 2021, status=404, error=Not Found, message=, path=/token/generate (truncated)...]
2021-02-16 23:00:56:084 [http-nio-127.0.0.1-8083-exec-3] DEBUG servlet.DispatcherServlet:1127 - Exiting from "ERROR" dispatch, status 404

This is my controller class of Service A:

@Slf4j
@CrossOrigin
@RestController
@RequestMapping(
        value = "/token")
public class JwtAuthenticationController {

    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    public JwtAuthenticationController(JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }

    @RequestMapping(value = "/get-token", method = RequestMethod.POST)
    public ResponseEntity<ResponseBody<TokenResponse>> getToken(@RequestBody TokenRequest tokenRequest) {
        log.debug("generating token");
        String username = tokenRequest.getUsername();
        String token = jwtTokenUtil.generateToken(username);
        TokenResponse response = new TokenResponse();
        response.setToken(token);
        return ResponseEntity.ok(new ResponseBody<>(response));
    }
}

This is Spring Security config class of Service A:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ServiceAConfigurerAdapter extends WebSecurityConfigurerAdapter {

    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Autowired
    public ServiceAConfigurerAdapter(JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint,
                                       JwtAuthenticationFilter jwtAuthenticationFilter) {
        this.jwtAuthenticationEntryPoint = jwtAuthenticationEntryPoint;
        this.jwtAuthenticationFilter = jwtAuthenticationFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().
                authorizeRequests()
                .antMatchers(
                        "/token/*")
                .permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

I can understand from log, for some reason, when calling from Service B, Spring Boot can't find the mapping of endpoint. But why, can't understand. Any suggestion, please?

** I am running both services in my localhost of windows machine.

Upvotes: 0

Views: 580

Answers (2)

shamim ehsan
shamim ehsan

Reputation: 119

Endpoints are different. From service A, you are calling /token/get-token. From service B, you are calling /token/generate-token. Please update the endpoints, then I think your code will work.

Upvotes: 1

Alam
Alam

Reputation: 371

From service B, you are calling /token/generate-token

whereas it should be /token/get-token

Upvotes: 1

Related Questions