Sni*_*ady 25 spring websocket http-status-code-403 sockjs
Spring中的WebSockets是一个相当新的话题,我很累.
我的问题是连接到来自不同域的服务,我正在使用Lineman构建前端端和Spring Boot做后端端,我将这些应用程序放在两个不同的端口上:8000和8080在localhost上.
我遇到了'Access-Control-Allow-Origin'标题的问题但我通过在服务器端添加一个过滤器来解决它,该过滤器将允许的原点添加到标题中.在此之后我开始在连接上收到以下错误:
GET http://localhost:8080/socket/info 403 (Forbidden)
AbstractXHRObject._start @ sockjs-0.3.4.js:807
(anonymous function) @sockjs-0.3.4.js:841
Run Code Online (Sandbox Code Playgroud)
我在项目中没有Spring Security,因此这不是授权问题,错误指向sockJS:that.xhr.send(payload); - 从未定义有效负载.我尝试过但无法找到可能开始的呼叫根.
我想在设置连接时是否需要向SockJS和Stomp添加一些额外的信息,但是这个工具的两个wiki页面中没有太多的示例和注释.
你会发现Bellow连接JS代码.
var socket = new SockJS("http://localhost:8080/socket");
client = Stomp.over(socket);
client.connect({'login': BoatsGame.userName,
'passcode': 'guest'},
function (frame) {
....
The Server Side has a MessageBroker configured :
@Configuration
@EnableWebSocketMessageBroker
public class MessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
//config.enableStompBrokerRelay("/queue", "/topic");
config.enableSimpleBroker("/queue", "/topic","/user");
config.setApplicationDestinationPrefixes("/BoatBattleGame");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/socket").withSockJS();
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试设置MessageHandler,因为它可以在配置时设置OriginAllowe,但我不确定它是如何连接到代理的.
最后想一想,在一个端口上运行时,此设置可正常工作.
Sni*_*ady 64
Jax的anwesr是正确的:)
registerStompEndpoints方法使我们有机会设置允许的起源.我们需要在"withSockJs()"选项之前添加它.
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/BO/socket").setAllowedOrigins("*").withSockJS();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
尝试通过SockJsClient连接到不同的域时,由于403 Forbidden答案而获得此票证的任何人:
尝试对/ info Url进行GET时会出现问题,这是握手的一部分.响应实际上通过WGET以及浏览器返回200.只有通过SockJsClient才行不通.
在尝试不同的解决方案后,唯一真正解决问题的方法是编写一个实现Transport和InfoReceiver的类.通过这种方式,开发人员可以直接处理握手的这一部分.基本上你在executeInfoRequest()方法中完成工作:
@Override
public String executeInfoRequest(URI infoUrl, HttpHeaders headers) {
HttpGet getRequest = new HttpGet(infoUrl); // eventually add headers here
HttpClient client = HttpClients.createDefault();
try {
HttpResponse response = client.execute(getRequest);
List<String> responseOutput = IOUtils.readLines(response.getEntity().getContent());
return responseOutput.get(0);
} catch (IOException ioe) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我将TransportType.XHR定义为传输类型.
就我而言,我必须添加这些配置才能使 SockJS / STOM 与 CORS 配合使用:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer
{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(false)
.maxAge(3600)
.allowedHeaders("Accept", "Content-Type", "Origin",
"Authorization", "X-Auth-Token")
.exposedHeaders("X-Auth-Token", "Authorization")
.allowedMethods("POST", "GET", "DELETE", "PUT", "OPTIONS");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25656 次 |
| 最近记录: |