Cannot invoke"org.springframework.web.client.RestTemplate.getForEntity(String, java.lang.Class, Object[])""this.restTemplate"null

huangapple go评论132阅读模式
英文:

Cannot invoke"org.springframework.web.client.RestTemplate.getForEntity(String, java.lang.Class, Object[])""this.restTemplate"null

问题

我正在开发一个tokenValidationInterceptor,其中会向另一个微服务发送令牌验证请求,该微服务与Keycloak通信并返回正常状态或未授权状态。然后,我在prehandle函数中添加了一些逻辑,就是这样。当我尝试测试它时,我得到了这个错误:

  1. java.lang.NullPointerException: Cannot invoke "org.springframework.web.client.RestTemplate.getForEntity(String, java.lang.Class, Object[])" because "this.restTemplate" is null

TokenValidationInterceptor的preHandle方法:

  1. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  2. String token = request.getHeader(HttpHeaders.AUTHORIZATION);
  3. String verificationUrl = "http://localhost:8090/user/info?token=" + token;
  4. ResponseEntity<Boolean> verificationResponse = restTemplate.getForEntity(verificationUrl, Boolean.class);
  5. if (verificationResponse.getStatusCode() == HttpStatus.OK && verificationResponse.getBody() != null && verificationResponse.getBody()) {
  6. return true;
  7. } else {
  8. response.setStatus(HttpStatus.UNAUTHORIZED.value());
  9. return false;
  10. }
  11. }

Appconfig:

  1. @Configuration
  2. public class AppConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. registry.addInterceptor(new TokenValidationInterceptor());
  6. }
  7. @Bean
  8. public RestTemplate restTemplate() {
  9. return new RestTemplate();
  10. }
  11. }

另一个微服务中的令牌验证:

  1. public ResponseEntity<?> verifyToken(String token) {
  2. String keycloakUrl = "http://localhost:8080/auth/realms/lbv-realm/protocol/openid-connect/userinfo";
  3. if (token != null) {
  4. HttpHeaders headers = new HttpHeaders();
  5. headers.setBearerAuth(token);
  6. HttpEntity<String> requestEntity = new HttpEntity<>(headers);
  7. RestTemplate restTemplate = new RestTemplate();
  8. try {
  9. ResponseEntity<String> response = restTemplate.exchange(keycloakUrl, HttpMethod.GET, requestEntity, String.class);
  10. HttpStatus statusCode = response.getStatusCode();
  11. if (statusCode.is2xxSuccessful()) {
  12. return ResponseEntity.status(HttpStatus.OK).body(true);
  13. } else {
  14. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  15. }
  16. } catch (Exception ex) {
  17. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  18. }
  19. } else {
  20. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  21. }
  22. }

请注意,我已经将HTML编码的引号(&quot;)更改为普通的双引号(")以便于代码的理解。

英文:

i'm working on an tokenValidationInterceptor , where a request for token validation is send to an other micro-service which communicate with keycloak and send an ok status or unautorazed status , then i add some logic to the prehandle function and that's all , when i try to test it, i get this error :

  1. java.lang.NullPointerException: Cannot invoke &quot;org.springframework.web.client.RestTemplate.getForEntity(String, java.lang.Class, Object[])&quot; because &quot;this.restTemplate&quot; is null

TokenValidationInterceptor prehandle method

  1. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  2. String token = request.getHeader(HttpHeaders.AUTHORIZATION);
  3. String verificationUrl = &quot;http://localhost:8090/user/info?token=&quot; + token;
  4. ResponseEntity&lt;Boolean&gt; verificationResponse = restTemplate.getForEntity(verificationUrl, Boolean.class);
  5. if (verificationResponse.getStatusCode() == HttpStatus.OK &amp;&amp; verificationResponse.getBody() != null &amp;&amp; verificationResponse.getBody()) {
  6. return true;
  7. } else {
  8. response.setStatus(HttpStatus.UNAUTHORIZED.value());
  9. return false;
  10. }
  11. }

Appconfig

  1. @Configuration
  2. public class AppConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. registry.addInterceptor( new TokenValidationInterceptor());
  6. }
  7. @Bean
  8. public RestTemplate restTemplate() {
  9. return new RestTemplate();
  10. }
  11. }

and for the tokenValidation in the other micro-service

  1. public ResponseEntity&lt;?&gt; verifyToken(String token) {
  2. String keycloakUrl = &quot;http://localhost:8080/auth/realms/lbv-realm/protocol/openid-connect/userinfo&quot;;
  3. if (token != null) {
  4. HttpHeaders headers = new HttpHeaders();
  5. headers.setBearerAuth(token);
  6. HttpEntity&lt;String&gt; requestEntity = new HttpEntity&lt;&gt;(headers);
  7. RestTemplate restTemplate = new RestTemplate();
  8. try {
  9. ResponseEntity&lt;String&gt; response = restTemplate.exchange(keycloakUrl, HttpMethod.GET, requestEntity, String.class);
  10. HttpStatusCode statusCode = response.getStatusCode();
  11. if (statusCode.is2xxSuccessful()) {
  12. return ResponseEntity.status(HttpStatus.OK).body(true);
  13. } else {
  14. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  15. }
  16. } catch (Exception ex) {
  17. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  18. }
  19. } else {
  20. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
  21. }
  22. }

答案1

得分: 0

如果您的 restTemplate 为null,您可以通过构造函数或 @Autowired 注解来简单地注入 RestTemplate

  1. @Autowired
  2. private RestTemplate restTemplate;

或者

  1. public class YourClass {
  2. private final RestTemplate restTemplate;
  3. public YourClass(RestTemplate restTemplate) {
  4. this.restTemplate = restTemplate;
  5. }
  6. }
英文:

If your restTemplate is null,you can simply inject RestTemplate through constructor or @Autowired annotation.

  1. @Autowired
  2. private RestTemplate restTemplate;

or

  1. public class YourClass{
  2. private final RestTemplate restTemplate;
  3. public YourClass(RestTemplate restTemplate){
  4. this.restTemplate=restTemplate;
  5. }
  6. }

huangapple
  • 本文由 发表于 2023年7月13日 18:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定