Merge pull request #11 from fabioformosa/#6_fix_websocket_with_jwt_header

#6 fix websocket with jwt header
This commit is contained in:
Fabio Formosa
2020-05-08 01:13:26 +02:00
committed by GitHub
6 changed files with 25 additions and 12 deletions

View File

@@ -150,6 +150,9 @@ public class JwtTokenHelper {
return authHeader.substring(7);
}
if(request.getParameter("access_token") != null)
return request.getParameter("access_token");
return null;
}

View File

@@ -4,11 +4,14 @@ export class SocketOption{
brokerName : string;
reconnectionTimeout : number = 30000
constructor(socketUrl : string, topicName : string, brokerName : string = null, reconnectionTimeout : number = 30000){
getAccessToken: Function = () => null;
constructor(socketUrl : string, topicName : string, getAccessToken?: Function, brokerName : string = null, reconnectionTimeout : number = 30000){
this.socketUrl = socketUrl;
this.topicName = topicName;
this.brokerName = brokerName;
this.reconnectionTimeout = reconnectionTimeout;
this.getAccessToken = getAccessToken || (() => null);
}
}

View File

@@ -41,6 +41,8 @@ export class ApiService {
this.jwtToken = token;
}
getToken = () => this.jwtToken;
get(path: string, args?: any): Observable<any> {
const options = {
headers: this.headers,
@@ -50,8 +52,8 @@ export class ApiService {
if (args)
options['params'] = serialize(args);
if(this.jwtToken)
options.headers = options.headers.set('Authorization', `Bearer ${this.jwtToken}`);
// if(this.jwtToken)
// options.headers = options.headers.set('Authorization', `Bearer ${this.jwtToken}`);
return this.http.get(path, options)
.pipe(catchError(this.checkError.bind(this)));
@@ -75,8 +77,8 @@ export class ApiService {
withCredentials: true
}
if(this.jwtToken)
options.headers = options.headers.append('Authorization', `Bearer ${this.jwtToken}`);
// if(this.jwtToken)
// options.headers = options.headers.append('Authorization', `Bearer ${this.jwtToken}`);
const req = new HttpRequest(method, path, body, options);

View File

@@ -1,12 +1,12 @@
import { Injectable, OnInit } from '@angular/core';
import { WebsocketService } from '.';
import { WebsocketService, ApiService } from '.';
import { SocketOption } from '../model/SocketOption.model';
@Injectable()
export class LogsWebsocketService extends WebsocketService {
constructor(){
super(new SocketOption('/quartz-manager/logs', '/topic/logs'))
constructor(private apiService: ApiService){
super(new SocketOption('/quartz-manager/logs', '/topic/logs', apiService.getToken))
}
}

View File

@@ -1,12 +1,12 @@
import { Injectable, OnInit } from '@angular/core';
import { WebsocketService } from '.';
import { WebsocketService, ApiService } from '.';
import { SocketOption } from '../model/SocketOption.model';
@Injectable()
export class ProgressWebsocketService extends WebsocketService {
constructor(){
super(new SocketOption('/quartz-manager/progress', '/topic/progress'))
constructor(private apiService: ApiService){
super(new SocketOption('/quartz-manager/progress', '/topic/progress', apiService.getToken))
}
}

View File

@@ -109,7 +109,12 @@ export class WebsocketService {
connect = () => {
const headers = {};
this._socket.client = new SockJS(this._options.socketUrl);
let socketUrl = this._options.socketUrl;
if(this._options.getAccessToken())
socketUrl += `?access_token=${this._options.getAccessToken()}`
this._socket.client = new SockJS(socketUrl);
this._socket.stomp = Stomp.over(this._socket.client);
this._socket.stomp.connect(headers, this._socketListener, this._onSocketError);
this._socket.stomp.onclose = this.scheduleReconnection;