Java Code Examples for org.springframework.messaging.simp.stomp.StompHeaderAccessor#setUser()
The following examples show how to use
org.springframework.messaging.simp.stomp.StompHeaderAccessor#setUser() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketInterceptor.java From xechat with MIT License | 7 votes |
/** * 绑定用户信息 * * @param message * @param channel * @return */ @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { log.debug("进入拦截器 -> preSend"); StompHeaderAccessor stompHeaderAccessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (StompCommand.CONNECT.equals(stompHeaderAccessor.getCommand())) { User user = new User(); user.setUserId(UUIDUtils.create()); user.setUsername(SensitiveWordUtils.loveChina(stompHeaderAccessor.getFirstNativeHeader("username"))); user.setAvatar(stompHeaderAccessor.getFirstNativeHeader("avatar")); user.setAddress(stompHeaderAccessor.getFirstNativeHeader("address")); user.setStatus(UserStatusConstant.ONLINE); stompHeaderAccessor.setUser(user); log.debug("绑定用户信息 -> {}", user); } return message; }
Example 2
Source File: StompSubProtocolHandler.java From spring-analysis-note with MIT License | 6 votes |
private Message<byte[]> createDisconnectMessage(WebSocketSession session) { StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.DISCONNECT); if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); Principal user = getUser(session); if (user != null) { headerAccessor.setUser(user); } return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders()); }
Example 3
Source File: StompSubProtocolHandler.java From java-technology-stack with MIT License | 6 votes |
private Message<byte[]> createDisconnectMessage(WebSocketSession session) { StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.DISCONNECT); if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); Principal user = getUser(session); if (user != null) { headerAccessor.setUser(user); } return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders()); }
Example 4
Source File: WebSocketConfig.java From tutorial with MIT License | 5 votes |
@Override public Message<?> preSend(Message<?> message, MessageChannel channel) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (log.isTraceEnabled()) { log.trace("拦截器截获命令:" + accessor.getCommand()); } if (StompCommand.CONNECT.equals(accessor.getCommand())) { // websocket连接刚刚建立起来,需要验证用户身份;根据token识别用户 String token = null; String requestHeader = accessor.getNativeHeader("Authorization").get(0); if (requestHeader != null && requestHeader.startsWith("Bearer ")) { token = requestHeader.substring(7); } if (log.isTraceEnabled()) { log.trace("MyChannelInterceptorAdapter->preSend() ... " + requestHeader + "; token=" + token); } User user = null; if (token == null) { user = new User("GUEST"); } else { // 把token转变为用户 user = new User(token); } accessor.setUser(user); } return message; }
Example 5
Source File: AuthChannelInterceptorAdapter.java From joal with Apache License 2.0 | 5 votes |
@Override public Message<?> preSend(final Message<?> message, final MessageChannel channel) throws AuthenticationException { final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (StompCommand.CONNECT == accessor.getCommand()) { final String username = accessor.getFirstNativeHeader(USERNAME_HEADER); final String authToken = accessor.getFirstNativeHeader(TOKEN_HEADER); final Authentication user = webSocketAuthenticatorService.getAuthenticatedOrFail(username, authToken); accessor.setUser(user); } return message; }
Example 6
Source File: StompSubProtocolHandler.java From spring4-understanding with Apache License 2.0 | 5 votes |
private Message<byte[]> createDisconnectMessage(WebSocketSession session) { StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.DISCONNECT); if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(session.getPrincipal()); return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders()); }
Example 7
Source File: JwtWebSocketInterceptorAdapter.java From spring-boot-start-current with Apache License 2.0 | 4 votes |
@Override public Message< ? > preSend ( Message< ? > message , MessageChannel channel ) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor( message , StompHeaderAccessor.class ); if ( ObjectUtils.notEqual( StompCommand.CONNECT , accessor.getCommand() ) ) { return message; } final String authToken = accessor.getFirstNativeHeader( tokenHeader ); final String username = jwtTokenUtil.getUsernameFromToken( authToken ); LogUtils.getLogger().debug( "authToken : {},username : {}" , authToken , username ); if ( StringUtils.isEmpty( username ) ) { throw new AuthenticationCredentialsNotFoundException( "未授权" ); } if ( SecurityContextHolder.getContext().getAuthentication() == null ) { // 对于简单的验证,只需检查令牌的完整性即可。 您不必强制调用数据库。 由你自己决定 // 是否查询数据看情况,目前是查询数据库 UserDetails userDetails = this.userDetailsService.loadUserByUsername( username ); if ( jwtTokenUtil.validateToken( authToken , userDetails ) ) { UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( userDetails , null , userDetails.getAuthorities() ); // authentication.setDetails( new WebAuthenticationDetailsSource().buildDetails( request ) ); LogUtils.getLogger().debug( "authToken : {},username : {}" , authToken , username ); LogUtils.getLogger().debug( "该 " + username + "用户已认证WebSocket, 设置安全上下文" ); SecurityContextHolder.getContext().setAuthentication( authentication ); accessor.setUser( authentication ); } } if ( Objects.isNull( accessor.getUser() ) ) { throw new AuthenticationCredentialsNotFoundException( "未授权" ); } return message; }