public void configureClientInboundChannel(ChannelRegistration registration) { registration.interceptors(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (StompCommand.CONNECT.equals(accessor.getCommand())) { String token = accessor.getFirstNativeHeader("Auth-Token");// if (!StringUtils.isEmpty(token)) { String authToken = token.substring(tokenHead.length()); String username = jwtTokenUtil.getUserNameFromToken(authToken); if (!StringUtils.isEmpty(username)) UserDetails userDetails = userDetailsService.loadUserByUsername(username); if (jwtTokenUtil.validateToken(authToken, userDetails)) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authenticationToken); accessor.setUser(authenticationToken); } } } } return message; } }); }是怎么运行的
时间: 2023-12-25 18:05:43 浏览: 44
这是一个 Spring Boot 中 WebSocket 的配置代码。在这个配置中,我们注册了一个 ChannelInterceptor,它的主要作用是在客户端连接到服务器之前,验证客户端的身份,并在连接成功后,将用户的认证信息保存在 StompHeaderAccessor 中,以便后续的处理。
具体来说,在客户端连接到服务器时,会发送一个 CONNECT 命令,该拦截器会从消息头中获取 "Auth-Token",并解析出其中的用户名和认证信息。然后,它会使用 Spring Security 的 UserDetailsService 加载该用户的 UserDetails,再使用 JWT TokenUtil 验证该用户的认证信息是否有效。如果验证通过,则创建一个 UsernamePasswordAuthenticationToken 对象,并使用 SecurityContextHolder 将其设置为当前的认证信息。
最后,将这个 authenticationToken 对象设置为 StompHeaderAccessor 的用户,并返回处理后的消息对象。这样,在后续的处理中,我们就可以从 StompHeaderAccessor 中获取到用户的认证信息了。
相关问题
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Resource private RedisTemplate redisTemplate; @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/stomp") //.setAllowedOrigins("") .setAllowedOriginPatterns("") .withSockJS(); } /** * 配置相关信息 * * @param registry */ @Override public void configureMessageBroker(MessageBrokerRegistry registry) { //表明在topic、queue这两个域上可以向客户端广播消息 registry.enableSimpleBroker("/topic", "/user"); //客户端向服务端发起请求时,需要以/app为前缀 registry.setApplicationDestinationPrefixes("/app"); //给指定用户发送一对一的消息前缀是/user registry.setUserDestinationPrefix("/user"); } @Override public void configureClientInboundChannel(ChannelRegistration registration) { registration.interceptors(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (StompCommand.CONNECT.equals(accessor.getCommand())) { String authorization = accessor.getFirstNativeHeader("accessToken"); String userId = JWTUtil.getUserId(authorization); String verify = (String) redisTemplate.opsForValue().get(Constant.Store.REDIS_TOKEN_PREFIX + userId); if (null == verify) { throw new IllegalArgumentException("用户验证失败"); } } return message; } }); } } 帮我翻译下
这是一个WebSocket的配置类,使用了Spring框架中的@Configuration和@EnableWebSocketMessageBroker注解。其中,@Resource注解用于注入RedisTemplate对象。
类中的registerStompEndpoints方法用于注册STOMP端点,指定了连接的路径为“/stomp”,并且使用了SockJS作为传输协议。
configureMessageBroker方法用于配置消息代理,指定了可以向客户端广播消息的域名为“/topic”和“/user”,同时客户端向服务端发起请求时需要以“/app”为前缀。此外,该方法还配置了给指定用户发送一对一消息时的前缀为“/user”。
configureClientInboundChannel方法用于配置客户端入站通道,其中注册了一个ChannelInterceptor拦截器,用于在客户端发起连接请求时进行用户验证。具体实现是从StompHeaderAccessor中获取连接请求中的accessToken,解析出其中的userId,然后从Redis中获取对应的验证信息,如果验证失败则抛出IllegalArgumentException异常。
public ArrayList<Interceptor> Interceptors = new ArrayList<Interceptor>();
这段代码声明了一个名为Interceptors的ArrayList,其泛型参数为Interceptor类。ArrayList是Java中的一个动态数组,可以根据需要动态地增加或减少元素。在这里,Interceptors被初始化为空的ArrayList,并且可以添加Interceptor对象作为其元素。Interceptor是一个接口,需要实现其中的方法。因此,这个ArrayList可以用来存储实现了Interceptor接口的对象,并在需要时对它们进行处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)