wss认证模式:单向认证、双向认证
单向认证
客户端校验服务的证书
$server = new \swoole_websocket_server("0.0.0.0", 8001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$listener = $server->listen("0.0.0.0", 8002, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$listener->set([
'ssl_key_file' => __DIR__ . "\\server8002.crt",
'ssl_cert_file' => __DIR__ . "\\server8002.key.unsecure",
]);
$url = "wss://127.0.0.1:8002";
$context = stream_context_create([
"ssl" => [
"verify_peer" => true,
"verify_peer_name" => false,
"allow_self_signed" => true,
"cafile" => __DIR__ . "\\ca8002.crt",
]
]);
$client = new Client($url, [
'context' => $context,
]);
双向认证
$server = new \swoole_websocket_server("0.0.0.0", 8001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$listener = $server->listen("0.0.0.0", 8002, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$listener->set([
'ssl_verify_peer' => true,
'ssl_allow_self_signed' => true,
'ssl_key_file' => __DIR__ . "\\server8002.crt",
'ssl_cert_file' => __DIR__ . "\\server8002.key.unsecure",
'ssl_client_cert_file' => __DIR__ . "\\ca8002.crt",
]);
- 客户端配置(php textalk\websocket)
$url = "wss://127.0.0.1:8000";
$context = stream_context_create([
"ssl" => [
"verify_peer" => true,
"verify_peer_name" => false,
"allow_self_signed" => true,
"cafile" => __DIR__ . "\\ca8002.crt",
"local_cert" => __DIR__ . "\\client.crt",
"local_pk" => __DIR__ . "\\client.key.unsecure",
]
]);
$client = new Client($url, [
'context' => $context,
]);
|