本文整理汇总了Java中org.springframework.messaging.simp.stomp.StompHeaderAccessor.getFirstNativeHeader方法的典型用法代码示例。如果您正苦于以下问题:Java StompHeaderAccessor.getFirstNativeHeader方法的具体用法?Java StompHeaderAccessor.getFirstNativeHeader怎么用?Java StompHeaderAccessor.getFirstNativeHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.messaging.simp.stomp.StompHeaderAccessor
的用法示例。
在下文中一共展示了StompHeaderAccessor.getFirstNativeHeader方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: preSend
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
@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;
}
示例2: onSessionConnectEvent
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
/**
* React on new web socket connections.
* @param event The application event.
*/
@EventListener
@Async
public void onSessionConnectEvent(SessionConnectEvent event) {
StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
ActiveWebsocketSession session = new ActiveWebsocketSession(
sha.getSessionId(),
sha.getSessionAttributes().get(WebSocketConfig.IP_ADDRESS).toString(),
sha.getFirstNativeHeader(BROWSER),
sha.getFirstNativeHeader(BROWSER_MAJOR_VERSION),
sha.getFirstNativeHeader(CLIENT_OS),
sha.getFirstNativeHeader(CLIENT_OS_VERSION),
sha.getAcceptVersion(),
LocalDateTime.now());
activeWebSocketSessionRepository.save(session);
log.debug("New websocket connection {}",
sha.getSessionAttributes().get(WebSocketConfig.IP_ADDRESS));
}
示例3: authenticate
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
protected void authenticate(StompHeaderAccessor accessor) {
String authToken = accessor.getFirstNativeHeader(ServerConstants.X_AUTH_TOKEN);
if (log.isDebugEnabled() && StringUtils.isNotEmpty(authToken)) {
log.debug("Header auth token: " + authToken);
}
if (StringUtils.isNotBlank(authToken)) {
// set cached authenticated user back in the spring security context
Authentication authentication = preAuthAuthenticationManager.authenticate(new PreAuthenticatedAuthenticationToken(authToken, "N/A"));
if (log.isDebugEnabled()) {
log.debug("Adding Authentication to SecurityContext for WebSocket call: " + authentication);
}
SpringSecurityHelper.setAuthentication(authentication);
}
}
示例4: sessionContextChannelInterceptorAdapter
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
@Bean
public ChannelInterceptorAdapter sessionContextChannelInterceptorAdapter() {
return new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
StompCommand command = accessor.getCommand();
if (log.isDebugEnabled() && command != null) {
log.debug("StompCommand: " + command.toString());
}
String authToken = accessor.getFirstNativeHeader(ServerConstants.X_AUTH_TOKEN);
if (log.isDebugEnabled() && StringUtils.isNotEmpty(authToken)) {
log.debug("Header auth token: " + authToken);
}
if (StringUtils.isNotBlank(authToken)) {
// set cached authenticated user back in the spring security context
Authentication authentication = preAuthAuthenticationManager.authenticate(new PreAuthenticatedAuthenticationToken(authToken, "N/A"));
if (log.isDebugEnabled()) {
log.debug("Adding Authentication to SecurityContext for WebSocket call: " + authentication);
}
SpringSecurityHelper.setAuthentication(authentication);
}
return super.preSend(message, channel);
}
};
}
示例5: preSend
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
@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;
}
示例6: handleMessageToClient
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
/**
* Handle STOMP messages going back out to WebSocket clients.
*/
@Override
@SuppressWarnings("unchecked")
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
if (!(message.getPayload() instanceof byte[])) {
logger.error("Expected byte[] payload. Ignoring " + message + ".");
return;
}
StompHeaderAccessor stompAccessor = getStompHeaderAccessor(message);
StompCommand command = stompAccessor.getCommand();
if (StompCommand.MESSAGE.equals(command)) {
if (stompAccessor.getSubscriptionId() == null) {
logger.warn("No STOMP \"subscription\" header in " + message);
}
String origDestination = stompAccessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
if (origDestination != null) {
stompAccessor = toMutableAccessor(stompAccessor, message);
stompAccessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
stompAccessor.setDestination(origDestination);
}
}
else if (StompCommand.CONNECTED.equals(command)) {
this.stats.incrementConnectedCount();
stompAccessor = afterStompSessionConnected(message, stompAccessor, session);
if (this.eventPublisher != null && StompCommand.CONNECTED.equals(command)) {
try {
SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes());
SimpAttributesContextHolder.setAttributes(simpAttributes);
Principal user = session.getPrincipal();
publishEvent(new SessionConnectedEvent(this, (Message<byte[]>) message, user));
}
finally {
SimpAttributesContextHolder.resetAttributes();
}
}
}
byte[] payload = (byte[]) message.getPayload();
if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) {
Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message);
stompAccessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
Assert.notNull(stompAccessor, "Expected STOMP headers");
payload = errorMessage.getPayload();
}
sendToClient(session, stompAccessor, payload);
}