springmvc jwt 跳过websocket
时间: 2023-10-12 14:06:22 浏览: 111
springmvc-websocket
在Spring MVC中,要跳过WebSocket的认证和授权,可以通过配置Spring Security来实现。以下是一种可能的实现方式:
1. 首先,在WebSecurityConfigurerAdapter中配置Spring Security,将WebSocket请求排除在认证和授权之外。例如:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/websocket/**").permitAll() // 跳过WebSocket认证
.anyRequest().authenticated() // 其他请求需要认证
.and()
.formLogin()
.and()
.httpBasic();
}
}
```
2. 接下来,在WebSocketConfigurer中配置WebSocket相关的内容。例如:
```java
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myWebSocketHandler(), "/websocket")
.setAllowedOrigins("*");
}
@Bean
public WebSocketHandler myWebSocketHandler() {
return new MyWebSocketHandler();
}
}
```
以上配置中,`/websocket`是WebSocket的访问路径,`MyWebSocketHandler`是自定义的WebSocket处理器。
阅读全文