In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use websocket to obtain HttpSession in spring. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
An ordinary website, after the user logs in, the user information is stored in HttpSession. Now the website needs to add the live chat function, intends to use Websocket implementation, needs to get the HttpSession in Websocket to represent the user.
/ * *
* Communication
* @ param chatMessage
, /
@ MessageMapping ("/ chat")
Public void chat (HttpSession session, @ RequestBody ChatMessage chatMessage) {
User user = (User) session.get ("user"); / / error
ChatService.chat (user,chatMessage)
}
Ordinary injection of HttpSession is not possible, because after the Websocket connection is established, it will not carry sessionid every time the data is transferred as in the HTTP protocol.
Solution idea
During the Websocket connection establishment phase (which is still the HTTP protocol), the HTTP request is intercepted, the HttpSesion is obtained, and saved.
Realize
I wanted to write a class myself, but I found that Spring Websocket already provided such an interceptor HttpSessionHandshakeInterceptor, so you can use it directly.
Websocket proxy configuration
@ Configuration
@ EnableWebSocketMessageBroker
Public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@ Override
Public void configureMessageBroker (MessageBrokerRegistry config) {
Config.enableSimpleBroker ("/ chat")
Config.setApplicationDestinationPrefixes ("/ app")
}
@ Override
Public void registerStompEndpoints (StompEndpointRegistry registry) {
Registry.addEndpoint ("/ endpoint") .setAllowedOrigins ("*") .addInterceptors (new HttpSessionHandshakeInterceptor ()) .withSockJS ()
}
}
This is similar to a normal Websocket configuration, with the biggest difference being addInterceptors (new HttpSessionHandshakeInterceptor ()), which adds HttpSessionHandshakeInterceptor to the interception chain.
We can take a look at the relevant methods in the HttpSessionHandshakeInterceptor source code.
/ / before the handshake is completed (connection establishment phase)
Public boolean beforeHandshake (ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception {
HttpSession session = this.getSession (request)
If (session! = null) {
If (this.isCopyHttpSessionId ()) {
Attributes.put ("HTTP.SESSION.ID", session.getId ()); / / Save sessionid
}
Enumeration names = session.getAttributeNames ()
While (true) {
String name
Do {
If (! names.hasMoreElements ()) {
Return true
}
Name = (String) names.nextElement ()
} while (! this.isCopyAllAttributes () & &! this.getAttributeNames () .contains (name))
Attributes.put (name, session.getAttribute (name)); / / Save the information in HttpSession
}
} else {
Return true
}
}
/ / obtain HttpSession
Private HttpSession getSession (ServerHttpRequest request) {
If (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request
Return serverRequest.getServletRequest () .getSession (this.isCreateSession ()
} else {
Return null
}
}
We can know from the source code that HttpSessionHandshakeInterceptor saves the values in HttpSession to a Map. By searching the official documents of spring, I found that I can get those values in the Controller method by injecting SimpMessageHeaderAccessor.
/ * *
* Communication
* @ param chatMessage
, /
@ MessageMapping ("/ chat")
Public void chat (SimpMessageHeaderAccessor headerAccessor, @ RequestBody ChatMessage chatMessage) {
User user = (User) headerAccessor.getSessionAttributes () .get ("user") / / right
ChatService.chat (user,chatMessage)
}
The above is how to use websocket to get HttpSession in the spring shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.