Tabnine Logo
HttpSessionHandshakeInterceptor.beforeHandshake
Code IndexAdd Tabnine to your IDE (free)

How to use
beforeHandshake
method
in
org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor

Best Java code snippets using org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor.beforeHandshake (Showing top 12 results out of 315)

origin: spring-projects/spring-framework

@Test
public void doNotCauseSessionCreation() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertNull(this.servletRequest.getSession(false));
}
origin: spring-projects/spring-framework

@Test
public void constructorWithAttributeNames() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  this.servletRequest.setSession(new MockHttpSession(null, "123"));
  this.servletRequest.getSession().setAttribute("foo", "bar");
  this.servletRequest.getSession().setAttribute("bar", "baz");
  Set<String> names = Collections.singleton("foo");
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertEquals(2, attributes.size());
  assertEquals("bar", attributes.get("foo"));
  assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
origin: spring-projects/spring-framework

@Test
public void doNotCopyHttpSessionId() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  this.servletRequest.setSession(new MockHttpSession(null, "123"));
  this.servletRequest.getSession().setAttribute("foo", "bar");
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  interceptor.setCopyHttpSessionId(false);
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertEquals(1, attributes.size());
  assertEquals("bar", attributes.get("foo"));
}
origin: spring-projects/spring-framework

@Test
public void doNotCopyAttributes() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  this.servletRequest.setSession(new MockHttpSession(null, "123"));
  this.servletRequest.getSession().setAttribute("foo", "bar");
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  interceptor.setCopyAllAttributes(false);
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertEquals(1, attributes.size());
  assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
origin: spring-projects/spring-framework

@Test
public void defaultConstructor() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  this.servletRequest.setSession(new MockHttpSession(null, "123"));
  this.servletRequest.getSession().setAttribute("foo", "bar");
  this.servletRequest.getSession().setAttribute("bar", "baz");
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertEquals(3, attributes.size());
  assertEquals("bar", attributes.get("foo"));
  assertEquals("baz", attributes.get("bar"));
  assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
origin: iSafeBlue/TrackRay

@Override
public boolean beforeHandshake(ServerHttpRequest request,
                ServerHttpResponse response, WebSocketHandler wsHandler,
                Map<String, Object> attributes) throws Exception {
  return super.beforeHandshake(request, response, wsHandler, attributes);
}
origin: gem-team/gemframe

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object>
    attributes) throws Exception {
  System.out.println("连接成功之前的拦截器1");
  return super.beforeHandshake(request, response, wsHandler, attributes);
}
origin: heibaiying/spring-samples-for-all

  @Override
  public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    InetSocketAddress remoteAddress = request.getRemoteAddress();
    InetAddress address = remoteAddress.getAddress();
    System.out.println(address);
    /*
     * 最后需要要显示调用父类方法,父类的beforeHandshake方法
     * 把ServerHttpRequest 中session中对应的值拷贝到WebSocketSession中。
     * 如果我们没有实现这个方法,我们在最后的handler处理中 是拿不到 session中的值
     * 作为测试 可以注释掉下面这一行 可以发现自定义处理器中session的username总是为空
     */
    return super.beforeHandshake(request, response, wsHandler, attributes);
  }
}
origin: lcw2004/one

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
  logger.info("WebSocket Handshake Before: " + request.getURI());
  try {
    if (request instanceof ServletServerHttpRequest) {
      ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
      // 获取凭证ID,检查用户是否登录
      String principalId = getPrincipalId(servletRequest.getServletRequest());
      // 从Session中获取CookieId(无法从Header中获取)
      String token = LoginUserUtils.getTokenFromSession(servletRequest.getServletRequest());
      logger.debug("WebSocket PrincipalId [{}]", principalId);
      logger.debug("WebSocket Token [{}]", token);
      attributes.put("principalId", principalId);
      attributes.put("token", token);
      return super.beforeHandshake(request, response, wsHandler, attributes);
    }
  } catch (LoginInvalidException e) {
    logger.warn("WebSocket Handshake Error(登录失效,握手失败)");
    return false;
  } catch (Exception e) {
    logger.warn("WebSocket Handshake Error({})", e.getMessage());
    logger.error(e.getMessage(), e);
    return false;
  }
  return false;
}
origin: nosqlcoco/springboot-weapp-demo

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
  
  if (request instanceof ServletServerHttpRequest) {
    //解决The extension [x-webkit-deflate-frame] is not supported问题
    if(request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {
      request.getHeaders().set("Sec-WebSocket-Extensions", "permessage-deflate");
    }
    ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
    HttpSession session = servletRequest.getServletRequest().getSession();
    if (session != null) {
      //使用user区分WebSocketHandler,以便定向发送消息
      HttpServletRequest req = servletRequest.getServletRequest();
      String name = req.getParameter("name");
      System.out.println(JSON.toJSONString(req.getParameterNames()));
      if(null != name && !"".equals(name)){
        attributes.put("user", name);
      }
      
    }
  }
  return super.beforeHandshake(request, response, wsHandler, attributes);
}
origin: choerodon/choerodon-starters

@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
                WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
  super.beforeHandshake(serverHttpRequest, serverHttpResponse, wsHandler, attributes);
  HttpSession httpSession = getRequestSession(serverHttpRequest);
  if (httpSession == null) {
    throw new HandshakeFailureException("Can not get HttpSession");
  }
  ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
  HttpServletRequest request = servletRequest.getServletRequest();
  request.getParameterMap().forEach(
      (k, v) -> {
        if (v.length == 1) {
          String t = v[0];
          attributes.put(k, t);
        } else {
          attributes.put(k, v);
        }
      }
  );
  String uuid = UUID.randomUUID().toString().replaceAll("-","");
  attributes.put(SESSION_ID,uuid);
  try {
    securityCheckManager.check(servletRequest);
  } catch (HandshakeFailureException e) {
    logger.warn("handshake failed: {}", e.getMessage());
    return false;
  }
  return true;
}
origin: com.hotels.road/road-offramp-service

attributes.put(GRANTS, grants);
return super.beforeHandshake(request, response, wsHandler, attributes);
org.springframework.web.socket.server.supportHttpSessionHandshakeInterceptorbeforeHandshake

Popular methods of HttpSessionHandshakeInterceptor

  • <init>
    Constructor for copying specific HTTP session attributes and the HTTP session id.
  • afterHandshake
  • getAttributeNames
    Return the configured attribute names to copy (read-only).
  • getSession
  • isCopyAllAttributes
    Whether to copy all HTTP session attributes.
  • isCopyHttpSessionId
    Whether to copy the HTTP session id to the handshake attributes.
  • isCreateSession
    Whether the HTTP session is allowed to be created.
  • setCopyAllAttributes
    Whether to copy all attributes from the HTTP session. If set to "true", any explicitly configured at
  • setCopyHttpSessionId
    Whether the HTTP session id should be copied to the handshake attributes under the key #HTTP_SESSION

Popular in Java

  • Finding current android device location
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • getExternalFilesDir (Context)
  • startActivity (Activity)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • Collections (java.util)
    This class consists exclusively of static methods that operate on or return collections. It contains
  • Dictionary (java.util)
    Note: Do not use this class since it is obsolete. Please use the Map interface for new implementatio
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • TreeMap (java.util)
    Walk the nodes of the tree left-to-right or right-to-left. Note that in descending iterations, next
  • ThreadPoolExecutor (java.util.concurrent)
    An ExecutorService that executes each submitted task using one of possibly several pooled threads, n
  • Best IntelliJ plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now