Post

๐Ÿ’ฅ Javascript URL Encoding

๐Ÿ’ฅ Javascript URL Encoding

๋ฌธ์ œ ์ƒํ™ฉ

โœ… Frontend ์ฝ”๋“œ

1
const socket = new SockJS('http://localhost:8083/ws?token=' + username);

โœ… Backend ์ฝ”๋“œ

1
2
3
4
5
6
7
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
	.setAllowedOriginPatterns("*")
	.setHandshakeHandler(customHandshakeHandler)
	.withSockJS();
}

โœ… Chrome ๊ฐœ๋ฐœ์ž ๋„๊ตฌ

  • WebSocket ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ–ˆ์ง€๋งŒ ์‹คํŒจํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ฒ˜์Œ์—๋Š” ์ฝ”๋“œ๋ฅผ ์•„๋ฌด๋ฆฌ ๋ด๋„ ๋ฌธ์ œ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค.

๋ฌธ์ œ ์›์ธ

  • ํ† ํฐ์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด ?? ๋ฌธ์ž์—ด์ธ๋ฐ, URL์„ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋Š” ๊ณต๋ฐฑ, =, &, ?, # ๊ฐ™์€ ํŠน์ˆ˜ ๋ฌธ์ž๋“ค์€ URL์—์„œ ์˜ˆ์•ฝ๋œ ๋ฌธ์ž๋กœ ์ „ํ™˜๋˜๋ฏ€๋กœ ์˜๋„ํ•œ ๋Œ€๋กœ ์š”์ฒญ์ด ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1
const socket = new SockJS('http://localhost:8083/ws?token=' + encodeURIComponent(username));
  • Frontend์—์„œ ์š”์ฒญ ์‹œ ์ „๋‹ฌํ•˜๋Š” Query String์˜ username์„ Encoding ํ•ด์•ผ ํ•œ๋‹ค.
This post is licensed under CC BY 4.0 by the author.