Я хочу использовать свой tokenRepository в моем WebSocketConfig
, который реализует WebSocketMessageBrokerConfigurer
В моем контроллере я могу получить tokenRepository
на @Autowired, but
websocketConfig` не может
Я переопределяю configureClientInboundChannel
в WebSocketConfig
, чтобы получить данные, когда пользователь подключает веб-сокет
В WebSocketConfig
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
logger.debug(">>>[CONFIG CLIENT INBOUND CHANNEL]");
registration.interceptors(new SelfChannelInterceptor());
}
В SelfChannelInterceptor
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
String token = accessor.getNativeHeader("token").get(0);
String id = accessor.getNativeHeader("id").get(0);
if (!validConnection(token, id)){
throw new IllegalArgumentException("No permission to this connection");
}
}
return message;
}
данные токена сохраняются в БД для проверки.
Но как я могу позволить WebSocketConfig
получить tokenRepository
Пример в tokenController
для проверки db
private JwtRepository jwtRepository;
private TokenService tokenService;
@Autowired
public TokenController(TokenRepository tokenRepository){
jwtRepository = new JwtRepository("xxxxxxx", "yyyyyyy");
tokenService = new TokenService(tokenRepository);
}
@GetMapping(path = "/all")
public @ResponseBody Iterable<Token> getAll(){
List<Token> tokens = tokenService.getAllTokens();
return tokens;
}