【问题标题】:springboot restcontroller redirect to Angular App URLspringboot restcontroller 重定向到 Angular App URL
【发布时间】:2018-09-06 16:49:03
【问题描述】:

我有一个场景,Springboot 应用程序接收到一个 [来自 IBM ISAM] 的 GET 请求,标头中带有 JWT 令牌。收到此请求后,Springboot 必须将其重定向到 Angular 应用程序并传递 JWT [在 Header 或 Query String 中]。

我正在 Springboot 中尝试这个,但在浏览器中没有发生重定向。

@RestController
@RequestMapping(value="/token")
public class RedirectController {

    @CrossOrigin
    @RequestMapping(value = "/redirect", method = RequestMethod.GET, produces = "application/json")
    public  ResponseEntity<Object> redirectToAngularApp(@RequestHeader HttpHeaders headerItems) {

        System.out.println("Inside redirect....");

        System.out.println("Header Content::"+headerItems.toString());
        String jwttoken = ""; 



        if (headerItems != null) {

            String tokenString = headerItems.getValuesAsList("authorization").get(0);

            System.out.println("TOKEN STRING::"+tokenString);
            // && headerItems.startsWith("Bearer ")
            jwttoken = tokenString.substring(7);

            System.out.println("JWT TOKEN::"+jwttoken);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setAccessControlAllowOrigin("*");
        headers.set("Access-Control-Allow-Methods","GET,OPTIONS,HEAD");
        headers.set("Access-Control-Allow-Headers", "Authorization");
        headers.set("Access-Control-Max-Age","86400");
        headers.setLocation(URI.create("http://localhost:4200/#/dpsportal?token="+jwttoken));
        return new ResponseEntity<>(headers, HttpStatus.FOUND);

    }

}

我在浏览器控制台中收到此错误。

Failed to load http://localhost:4200/#/: Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
jwt.interceptor.ts:111 ERROR::{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}
login.component.ts:68 TypeError: Observable_1.Observable.throw is not a function
    at CatchSubscriber.eval [as selector] (jwt.interceptor.ts:133)
    at CatchSubscriber.error (catchError.js:105)
    at XMLHttpRequest.onError (http.js:2302)

请帮助解决这个问题。

【问题讨论】:

    标签: angular spring-boot


    【解决方案1】:

    试试:

    @RestController
    public class FooController {
    
      @RequestMapping("/foo")
      void handleFoo(HttpServletResponse response) throws IOException {
        response.sendRedirect("some-url");
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2019-03-25
      • 1970-01-01
      • 2021-01-09
      • 2015-05-19
      • 2018-06-24
      • 2018-07-26
      • 2017-09-21
      相关资源
      最近更新 更多