springboot restcontroller重定向到Angular App URL

时间:2018-09-06 16:49:04

标签: angular spring-boot

我有一个场景,其中Springboot App接收到来自[IBM ISAM]的静态GET请求,该请求在Header中带有JWT令牌。收到此请求后,Springboot必须将其重定向到Angular App并传递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)

请帮助解决此问题。

0 个答案:

没有答案