本文整理汇总了Java中org.springframework.messaging.simp.stomp.StompHeaderAccessor.setUser方法的典型用法代码示例。如果您正苦于以下问题:Java StompHeaderAccessor.setUser方法的具体用法?Java StompHeaderAccessor.setUser怎么用?Java StompHeaderAccessor.setUser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.messaging.simp.stomp.StompHeaderAccessor
的用法示例。
在下文中一共展示了StompHeaderAccessor.setUser方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: createDisconnectMessage
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
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());
}
示例3: getPositions
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
@Test
public void getPositions() throws Exception {
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
headers.setSubscriptionId("0");
headers.setDestination("/app/positions");
headers.setSessionId("0");
headers.setUser(new TestPrincipal("fabrice"));
headers.setSessionAttributes(new HashMap<String, Object>());
Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
this.clientOutboundChannelInterceptor.setIncludedDestinations("/app/positions");
this.clientInboundChannel.send(message);
Message<?> reply = this.clientOutboundChannelInterceptor.awaitMessage(5);
assertNotNull(reply);
StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(reply);
assertEquals("0", replyHeaders.getSessionId());
assertEquals("0", replyHeaders.getSubscriptionId());
assertEquals("/app/positions", replyHeaders.getDestination());
String json = new String((byte[]) reply.getPayload(), Charset.forName("UTF-8"));
new JsonPathExpectationsHelper("$[0].company").assertValue(json, "Citrix Systems, Inc.");
new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
}
示例4: executeTrade
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
@Test
public void executeTrade() throws Exception {
Trade trade = new Trade();
trade.setAction(Trade.TradeAction.Buy);
trade.setTicker("DELL");
trade.setShares(25);
byte[] payload = new ObjectMapper().writeValueAsBytes(trade);
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setDestination("/app/trade");
headers.setSessionId("0");
headers.setUser(new TestPrincipal("fabrice"));
headers.setSessionAttributes(new HashMap<String, Object>());
Message<byte[]> message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
this.brokerChannelInterceptor.setIncludedDestinations("/user/**");
this.clientInboundChannel.send(message);
Message<?> positionUpdate = this.brokerChannelInterceptor.awaitMessage(5);
assertNotNull(positionUpdate);
StompHeaderAccessor positionUpdateHeaders = StompHeaderAccessor.wrap(positionUpdate);
assertEquals("/user/fabrice/queue/position-updates", positionUpdateHeaders.getDestination());
String json = new String((byte[]) positionUpdate.getPayload(), Charset.forName("UTF-8"));
new JsonPathExpectationsHelper("$.ticker").assertValue(json, "DELL");
new JsonPathExpectationsHelper("$.shares").assertValue(json, 75);
}
示例5: createMessage
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
private Message<byte[]> createMessage(String type, String destination, String data, UUID userId) throws Exception {
byte[] payload = null;
if(data != null) {
//Create a MapMessage object for message payload
MapMessage mapMessage = new MapMessage();
mapMessage.setType(type);
mapMessage.setData(data);
payload = new ObjectMapper().writeValueAsBytes(mapMessage);
} else {
//Create an BeanMessage object for message payload
BeanMessage beanMessage = new BeanMessage();
beanMessage.setType(type);
payload = new ObjectMapper().writeValueAsBytes(beanMessage);
}
//Create headers
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setDestination(destination);
headers.setSessionId("0");
headers.setSessionAttributes(new HashMap<String, Object>());
//Generate a user
headers.setUser(new TestPrincipal(userId == null ? this.uuid : userId.toString()));
//Build the message (with a payload and headers)
Message<byte[]> message = MessageBuilder.withPayload(payload).setHeaders(headers).build();
return message;
}
示例6: 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;
}
示例7: createMatchMessage
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; //导入方法依赖的package包/类
private Message<byte[]> createMatchMessage(String type, String destination, String appId, UUID userId) throws Exception {
//Create a device
Device device = new Device(userId == null ? this.uuid : userId.toString());
device.setHeight(400);
device.setWidth(400);
byte[] payload = null;
switch(type) {
case "join":
//Create a JoinMessage object for message payload
JoinMessage joinMessage = new JoinMessage();
joinMessage.setDevice(device);
joinMessage.setGeo(new float[]{0, 0});
joinMessage.setType(JoinMessage.Types.exit);
joinMessage.setApplicationId(appId);
payload = new ObjectMapper().writeValueAsBytes(joinMessage);
break;
case "pair":
//Create a PairMessage object for message payload
PairMessage pairMessage= new PairMessage();
pairMessage.setDevice(device);
pairMessage.setGeo(new float[]{0, 0});
pairMessage.setType(PairMessage.Types.exit);
pairMessage.setApplicationId(appId);
payload = new ObjectMapper().writeValueAsBytes(pairMessage);
break;
default:
return null;
}
//Create headers
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setDestination(destination);
headers.setSessionId("0");
headers.setSessionAttributes(new HashMap<String, Object>());
//Generate a user
headers.setUser(new TestPrincipal(userId == null ? this.uuid : userId.toString()));
//Build the message (with a payload and headers)
Message<byte[]> message = MessageBuilder.withPayload(payload).setHeaders(headers).build();
return message;
}