我正在使用 Spring Boot 和 Angular 构建网络聊天。为此,我将 Websockets 与客户端 SockJS 和 Webstomp 以及服务器端 Spring 结合使用。
问题是我在运行时在控制台上收到此错误:
弃用:未定义不是公认的 STOMP 版本。在下一个主要客户端版本中,这将关闭连接。
我在 google 上查了一下,在 github stomp 项目上找到了这个线程:https : //github.com/JSteunou/webstomp-client/issues/75
这是与 SockJS 和 Webstomp 的协议兼容性问题。
所以,我在下面的代码中添加了options参数(即使协议参数不允许用于 over 方法?)
var socket = new SockJS("http://localhost:8086/ws");
var options = {debug: false, protocols: webstomp.VERSIONS.supportedProtocols()};
var stompClient = webstomp.over(socket, options);
Run Code Online (Sandbox Code Playgroud)
并且错误信息消失了。但是在请求标头中仍然没有标头 Sec-Websocket-Procotol 来定义使用哪个协议,我想我将来会遇到问题。
我该怎么做才能使我的代码正常工作?
客户端(角度)
connectWebSocketRoom() {
var socket = new SockJS("http://localhost:8086/ws");
var options = {debug: false, protocols: webstomp.VERSIONS.supportedProtocols()};
var stompClient = webstomp.over(socket, options);
// var …Run Code Online (Sandbox Code Playgroud) 我的自定义过滤器基于UsernamePasswordAuthenticationFilter需求,AuthenticationManager但每次我调用该方法时,attemptAuthentication()编译都会失败:
Authentication auth2 = this.getAuthenticationManager().authenticate(authRequest);
Run Code Online (Sandbox Code Playgroud)
为AuthenticationManager空:
java.lang.NullPointerException: null
at app.shellx.security.CustomUsernamePasswordAuthenticationFilter.attemptAuthentication(CustomUsernamePasswordAuthenticationFilter.java:75) ~[classes/:na]
Run Code Online (Sandbox Code Playgroud)
网络安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
@Autowired
private JwtTokenFilter jwtTokenFilter;
@Autowired
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class)
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.cors().and()
.csrf().disable()
.authorizeRequests() // .antMatchers("/**")
.antMatchers("/login/**", "/register/**").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
//.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
.addFilterAt(new CustomUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.formLogin()
.loginPage("http://localhost:4200/login")//.failureUrl("/login-error")
.loginProcessingUrl("/login")
.usernameParameter("email")
.successHandler(customAuthenticationSuccessHandler)
.and()
.logout()
.permitAll();
} …Run Code Online (Sandbox Code Playgroud)