【发布时间】:2016-11-02 19:43:37
【问题描述】:
一开始我很抱歉我的语言不好。我是一名来自德国的学生,刚接触编程。
我已经使用 Spring Boot 实现了一个小型 websocket 服务器,并使用 Basic Auth 使用 Spring Security 对其进行保护。 Angular 2 用于前端。我已经实现了以下连接到 websocket 的解决方案。
* Connects to a websocket server. * @param {string} url The url of the WS Server * @param {string} channel The channel to subscribe * @return {boolean} connection status */ public connect(url: string, channel: string): boolean { console.group('WebSocketService: Welcome to the connect function.'); console.log('Connects to', url); console.log('Channel is', channel); let _self = this; let socket = new SockJS(url);
_self.wsClient = Stomp.over(socket);
_self.wsClient.connect({}, function(frame) {
_self.setConnected(true);
console.log('Connected: ', frame);
_self.wsClient.subscribe(channel, function(greeting) {
console.log(greeting);
_self.subjects[channel].next(greeting);
});
});
console.groupEnd();
return true;
}
调用这个函数让浏览器打开用户名和密码的输入。 现在我可以连接和断开服务器,无需再次输入用户名/密码。
如何使用代码中的登录信息替换此浏览器输入窗口?
我尝试通过一个 Post 请求来做到这一点,例如:
private test2(){ let username : string = 'admin'; let password : string = 'pass'; let headers = new Headers(); headers.append("Authorization", "Basic " + btoa(username + ":" + password)); headers.append("Content-Type", "application/x-www-form-urlencoded"); console.log('Test beginnt'); return this._http.get('http://localhost:8080/gs-guide-websocket', {headers: headers}).map(res=> console.log(res)) }块引用
但这只给了我一个 http 200 响应,并没有打开会话或其他东西。
我也尝试将登录信息放入原始请求中。
_self.wsClient.connect('admin','pass', function(frame) { ...}
感谢您的帮助。
【问题讨论】:
标签: spring angular spring-security sockjs