当前位置: 首页 > 知识库问答 >
问题:

登录应用程序后,在@OnOpen连接之前,检查java websocket身份验证

逑彬炳
2023-03-14

我正在实施avax.websocket更新我的应用程序中的消息计数如下。

我想保护websocketendpoint,我在web中使用了下面的。xml和weblogic。xml,但它不起作用。之后,我尝试使用自定义ServerEndpointConfig。Configurator,现在我可以在登录到我的应用程序后连接。但是会话在请求中是空的。

通过网络认证。xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1">
  <display-name>WebSocketPrj</display-name>
  <security-constraint>
    <web-resource-collection>
        <web-resource-name>simple web resources</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>TestUser</role-name>
    </auth-constraint>
  </security-constraint>
  <login-config>
    <auth-method>Basic</auth-method>
    <realm-name>file</realm-name>
  </login-config>
  <security-role>
    <role-name>TestUser</role-name>
  </security-role> 
</web-app>

网络日志。xml

<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
  <security-role-assignment>
    <role-name>TestUser</role-name>
    <principal-name>TestUser</principal-name>
  </security-role-assignment>
  <container-descriptor></container-descriptor>
</weblogic-web-app>

在网络中没有安全约束。没有weblogic的xml和xml。xml

public class WebSocketConfigurator extends ServerEndpointConfig.Configurator{
private static final String ORIGIN = "http://localhost:7001";

@Override
public boolean checkOrigin(String originHeaderValue) {
    return ORIGIN.equals(originHeaderValue);
}

@Override
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
    HttpSession httpSession = (HttpSession) request.getHttpSession();
    super.modifyHandshake(config, request, response);
    if (httpSession == null) {
        httpSession = (HttpSession) request.getHttpSession();
    }

    if (httpSession == null) {
        return;
    }

    config.getUserProperties().put("httpSession", httpSession);

    httpSession = (HttpSession) request.getHttpSession();
}

}

@ServerEndpoint(value=“/messageCount”,configurator=WebSocketConfigurator.class)公共类NotificationSocket{private static final Logger Logger=LoggerFactory.getLogger(NotificationSocket.class);

private Session wsSession;
private HttpSession httpSession;

/**
 *
 * @param session
 */
@OnOpen
public void onOpen(Session session, EndpointConfig config){

}

/**
 *
 */
@OnClose
public void onClose(){
    // implementation not needed
}

/**
 *
 * @param session
 * @param throwable
 */
@OnError
public void error(Session session,Throwable throwable){
    // implementation not needed
}

/**
 *
 * @param user
 * @param session
 */
@OnMessage
public void handleMessage(final String user, final Session session) {
    synchronized (session) {
        int count = 10;

        session.getAsyncRemote().sendText("" + count);

    }
}

}

我有登录页面,登录后welcomauth.jsp我调用下面的webocket:下面是我的javascript客户端:

var webSocket = new WebSocket("ws://localhost:7001/messageCount");
    webSocket.onopen=function(){
    webSocket.send('Message');
    }

我不想在modifyhandshake中从客户端传递用户信息,我想获取已登录的用户会话,然后验证用户是否存在,然后打开连接,否则抛出异常。(比如抛出新的RuntimeException(“未验证”))

我在登录应用程序后调用websocket。在websocket连接(即@OnOpen)之前,有没有办法检查身份验证?

共有1个答案

柳羽
2023-03-14

我解决了。我们需要添加一个servlet过滤器,并添加与websocket serverendpoint相同的url模式。

在Servlet过滤器中添加身份验证逻辑。因此,每当调用websocket url时,它都会点击过滤器并验证身份验证。

 类似资料:
  • 对于我的android应用程序,我使用的是google firebase auth,问题是在卸载和重新安装该应用程序后,它仍然跳过登录页面并进行会话,我使用的是android 7,我根据这里的答案清理了备份,在Manifest.xml中添加了和但没有帮助。有谁能帮上忙吗?

  • 我是Symfony2的新手,我正在尝试创建一个基本的注册系统。因此,在Symfony2文档的帮助下,我创建了这个security.yml: 我使用了这个路由: 根据http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form我不需要为login_check路由实现控制器。然而,Symfony将这个错

  • 使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。

  • 在loadByUserName()方法中,我通过邮件找到用户。目前,我的用户类使用isEnabled()方法实现UserDetails,以检查用户是否已启用。然而,在身份验证之前会检查这个启用标志,这样任何人都可以发现另一个用户的帐户是否启用,无论使用什么密码。有没有办法在身份验证后检查启用标志? 我的用户类: My loadUserByUsername()方法: 我更改了默认消息,以防凭据不良或

  • 我遵循了GitHub教程中的所有内容:https://help.github.com/articles/generating-ssh-keys 我在存储库的目录中执行了所有命令。我成功到达教程的末尾,并收到消息:“嗨,用户名!你已成功通过身份验证,但 GitHub 不提供外壳访问。 然而,当我尝试做推送之类的事情时,它仍然要求输入我的用户名和密码。

  • 我试图在登录symfony2应用程序后实现重定向,以便在我的用户是否有一个属性时重定向。我已在项目的Handler文件夹中创建了AuthenticationSuccessHandler.php类: 但是当我登录时,我得到一个错误: 注意:未定义的属性:Me\MyBundle\Handler\AuthenticationSuccessHandler::$router in/var/www/MyBun