【问题标题】:how to get id of current user in websocket open method?如何在 websocket 打开方法中获取当前用户的 ID?
【发布时间】:2015-10-15 20:38:14
【问题描述】:

我正在尝试在 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);

}

有人知道我应该怎么做吗?

【问题讨论】:

  • 我不熟悉 shiro,但我敢打赌 currentUser.getPrincipals... 的东西预计这是一个普通的 http 请求,其中包含 cookie 等。您可能需要通过 websocket 连接传递一个标识符来识别您的用户。什么意思?
  • 谢谢,我认为你是对的,我得想点办法了
  • 听起来不错。让我知道你发现了什么。希望我能提供更多帮助。

标签: java websocket shiro


【解决方案1】:

解决了一天后,我将 open 方法的类更改为:

@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 类:

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;
}

【讨论】:

    【解决方案2】:

    一个更简单的解决方案可能是使用 javax.websocket.Session.getUserPrincipal() 返回此会话的经过身份验证的用户,如果此会话没有用户经过身份验证,则返回 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 等后续方法也可以使用相同的方法来检索用户。

    【讨论】:

    • 对我来说,它返回 null,即使发出套接字请求的用户已经过身份验证。
    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 2015-06-01
    • 2022-08-02
    • 2012-12-21
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多