Spring websocket jwt认证
Spring websocket jwt authentication
我需要添加jwt认证。我有 jwt 生成和验证 class。我不知道如何在 websocket 中添加它。我认为 spring 必须在连接开始时检查 jwt 令牌
服务器端点class
@Component
@ServerEndpoint(value = "/user",
encoders = { MessageEncoder.class },
decoders = { MessageDecoder.class }
)
public class ChatEndpoint implements Serializable {
@OnOpen
public void OnOpen (Session session) throws IOException {
}
@OnMessage
public void onMessage(Session session, Message message) throws IOException, EncodeException {
}
@OnClose
public void onClose(Session session) throws IOException, EncodeException {
}
@OnError
public void onError(Session session, Throwable throwable) {
// Do error handling here
System.out.println(throwable);
}
public static void broadcast(Message message) throws IOException {
}
private void sendMessage(Message message) throws IOException {
}
}
首发class
@ServletComponentScan
@SpringBootApplication
public class WebsocketdemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(WebsocketdemoApplication.class);
app.run(args);
}
}
配置class
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter endpointExporter(){
return new ServerEndpointExporter();
}
}
我的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.4.3</version>
</dependency>
希望有人能帮助我。我想不出来。每个人都在使用 stomp,但我没有使用它,所以很奇怪
问题是在添加过滤器后过滤我可以安排 jwt 安全性
@WebFilter("/user")
public class AccessTokenFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
// jwt auth
}
}
我需要添加jwt认证。我有 jwt 生成和验证 class。我不知道如何在 websocket 中添加它。我认为 spring 必须在连接开始时检查 jwt 令牌
服务器端点class
@Component
@ServerEndpoint(value = "/user",
encoders = { MessageEncoder.class },
decoders = { MessageDecoder.class }
)
public class ChatEndpoint implements Serializable {
@OnOpen
public void OnOpen (Session session) throws IOException {
}
@OnMessage
public void onMessage(Session session, Message message) throws IOException, EncodeException {
}
@OnClose
public void onClose(Session session) throws IOException, EncodeException {
}
@OnError
public void onError(Session session, Throwable throwable) {
// Do error handling here
System.out.println(throwable);
}
public static void broadcast(Message message) throws IOException {
}
private void sendMessage(Message message) throws IOException {
}
}
首发class
@ServletComponentScan
@SpringBootApplication
public class WebsocketdemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(WebsocketdemoApplication.class);
app.run(args);
}
}
配置class
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter endpointExporter(){
return new ServerEndpointExporter();
}
}
我的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.4.3</version>
</dependency>
希望有人能帮助我。我想不出来。每个人都在使用 stomp,但我没有使用它,所以很奇怪
问题是在添加过滤器后过滤我可以安排 jwt 安全性
@WebFilter("/user")
public class AccessTokenFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
// jwt auth
}
}