如何在 websocket open 方法中获取当前用户的 id?
how to get id of current user in websocket open method?
我试图在 websocket 的 open 方法中获取用户 ID,为此我正在使用 shiro,但是我得到的 Subject 为 null,这是我的方法:
@OnOpen
public void open(final Session session, @PathParam("room") final String room) {
Subject currentUser = SecurityUtils.getSubject();
long id = currentUser.getPrincipals().oneByType(model.Users.class)
.getId();
log.info("session openend and bound to room: " + room);
session.getUserProperties().put("user", id);
}
有人知道我应该做什么吗?
解决了一天后,我把打开方式class改成这样:
@ServerEndpoint(value = "/chat/{room}", configurator = GetHttpSessionConfigurator.class, encoders = ChatMessageEncoder.class, decoders = ChatMessageDecoder.class)
public class ChatEndpoint {
private final Logger log = Logger.getLogger(getClass().getName());
@OnOpen
public void open(final Session session,
@PathParam("room") final String room, EndpointConfig config) {
log.info("session openend and bound to room: " + room);
Principal userPrincipal = (Principal) config.getUserProperties().get(
"UserPrincipal");
String user=null;
try {
user = (String) userPrincipal.getName();
} catch (Exception e) {
e.printStackTrace();
}
session.getUserProperties().put("user", user);
System.out.println("!!!!!!!! "+user);
}}
和 GetHttpSessionConfigurator class:
public class GetHttpSessionConfigurator extends
ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request, HandshakeResponse response) {
config.getUserProperties().put("UserPrincipal",request.getUserPrincipal());
config.getUserProperties().put("userInRole", request.isUserInRole("someRole"));
}}
并从 Principal 实现我的用户模型并覆盖 getName() 方法以获取 ID:
@Override
public String getName() {
String id=Long.toString(getId());
return id;
}
一个更简单的解决方案可能是使用 javax.websocket.Session.getUserPrincipal(),其中 Returns 此会话的经过身份验证的用户,如果没有用户经过此会话的身份验证,则为 null 像这样,
@OnOpen
public void open(final Session session, @PathParam("room") final String room) {
Principal userPrincipal = session.getUserPrincipal();
if (userPrincipal == null) {
log.info("The user is not logged in.");
} else {
String user = userPrincipal.getName();
// now that your have your user here, your can do whatever other operation you want.
}
}
后续的onMessage等方法也可以使用相同的方法获取用户。
我试图在 websocket 的 open 方法中获取用户 ID,为此我正在使用 shiro,但是我得到的 Subject 为 null,这是我的方法:
@OnOpen
public void open(final Session session, @PathParam("room") final String room) {
Subject currentUser = SecurityUtils.getSubject();
long id = currentUser.getPrincipals().oneByType(model.Users.class)
.getId();
log.info("session openend and bound to room: " + room);
session.getUserProperties().put("user", id);
}
有人知道我应该做什么吗?
解决了一天后,我把打开方式class改成这样:
@ServerEndpoint(value = "/chat/{room}", configurator = GetHttpSessionConfigurator.class, encoders = ChatMessageEncoder.class, decoders = ChatMessageDecoder.class)
public class ChatEndpoint {
private final Logger log = Logger.getLogger(getClass().getName());
@OnOpen
public void open(final Session session,
@PathParam("room") final String room, EndpointConfig config) {
log.info("session openend and bound to room: " + room);
Principal userPrincipal = (Principal) config.getUserProperties().get(
"UserPrincipal");
String user=null;
try {
user = (String) userPrincipal.getName();
} catch (Exception e) {
e.printStackTrace();
}
session.getUserProperties().put("user", user);
System.out.println("!!!!!!!! "+user);
}}
和 GetHttpSessionConfigurator class:
public class GetHttpSessionConfigurator extends
ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request, HandshakeResponse response) {
config.getUserProperties().put("UserPrincipal",request.getUserPrincipal());
config.getUserProperties().put("userInRole", request.isUserInRole("someRole"));
}}
并从 Principal 实现我的用户模型并覆盖 getName() 方法以获取 ID:
@Override
public String getName() {
String id=Long.toString(getId());
return id;
}
一个更简单的解决方案可能是使用 javax.websocket.Session.getUserPrincipal(),其中 Returns 此会话的经过身份验证的用户,如果没有用户经过此会话的身份验证,则为 null 像这样,
@OnOpen
public void open(final Session session, @PathParam("room") final String room) {
Principal userPrincipal = session.getUserPrincipal();
if (userPrincipal == null) {
log.info("The user is not logged in.");
} else {
String user = userPrincipal.getName();
// now that your have your user here, your can do whatever other operation you want.
}
}
后续的onMessage等方法也可以使用相同的方法获取用户。