Home > database >  How to send secure url from Angular 8 to spring boot controller?
How to send secure url from Angular 8 to spring boot controller?

Time:12-29

I'm trying to hit the controller from the template, my http client calls are automatically added token but this link doesn't work, so I tried to add pipe in order for it to make httpClient call but it doesn't work.

I have the following template in Angular 8 app:

<a href="MY_API   '/my/url/'   myId | secureLink | async" target="_blank">Download PDF</a>

This is the pipe:

@Pipe({
    name: 'secureLink'
})
export class SecureLinkPipe implements PipeTransform {
    constructor(private http: HttpClient, private sanitizer: DomSanitizer) { }
    transform(url): Observable<SafeUrl> {
        return this.http
            .get(url, { responseType: 'text' })
            .map(val => this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(val)));
    }
}

If the pipe was working correctly I supposed to hit the controller:

@GetMapping("/my/url/{myId}")   
    public ResponseEntity<byte[]> generatePDF(@PathVariable("myId") String myId) {

Without pipe I was able to reach the JWT filter but it is null of course. Can you help with tranforming the url to http client call?

CodePudding user response:

HttpInterceptor only works with HttpClient method calls. Anchor links wouldn't get intercepted so wouldn't have the auth token added

You can create a link button or intercept the anchor click (an anchor with no href) and call the API from the click handler

CodePudding user response:

The problem is not from your pipe. It's from how you use it. This line is wrong:
<a href="MY_API '/my/url/' myId | secureLink | async" target="_blank">Download PDF</a>

You're using a normal text. What you needed to do is to bind the value to the href attribute.

  • Normal text: href="1 1" => result: href="1 1", literal, nothing change
  • Binding: [href]="1 1" => result: href="2"

So to fix the issue, this is what you should use:
"<a [href]="MY_API '/my/url/' myId | secureLink | async" target="_blank">Download PDF"

  • Related