Java 来自Postman的API调用正在工作,但来自另一个使用改进的服务的API调用不工作
我建立了一个服务(服务a)。假设我正在调用一个API来获取令牌。这对邮递员来说很有效。以下是我的回应: 那么服务A的日志如下所示:Java 来自Postman的API调用正在工作,但来自另一个使用改进的服务的API调用不工作,java,spring-boot,rest,spring-security,Java,Spring Boot,Rest,Spring Security,我建立了一个服务(服务a)。假设我正在调用一个API来获取令牌。这对邮递员来说很有效。以下是我的回应: 那么服务A的日志如下所示: 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
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
但是当从服务B调用相同的API时,我在服务A中得到了404。那么服务A的日志是:
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
这是我的服务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));
}
}
@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);
}
}
我可以从日志中了解到,由于某种原因,当从服务B调用时,Spring Boot无法找到端点的映射。但是为什么,我不明白。有什么建议吗
**我正在windows机器的本地主机上运行这两个服务。从服务B调用/token/generate token
虽然它应该是/token/get token来自服务B,但您正在调用/token/generate token 而应该是/token/get token端点是不同的。 从服务A,您正在呼叫/token/get token。 从服务B,您正在调用/token/generate token。 请更新端点,然后我认为您的代码可以工作。端点不同。 从服务A,您正在呼叫/token/get token。 从服务B,您正在调用/token/generate token。
请更新端点,然后我认为您的代码会工作。非常感谢,伙计!不知道,怎么错过了。浪费了几乎两个小时检查几乎所有类型的配置!非常感谢,伙计!不知道,怎么错过了。浪费了几乎两个小时检查几乎所有类型的配置!