跳到主要内容

actix-web htts配置

看证书加密算法

网址:https://myssl.com/cert_decode.html 上传ca_bundle.crt文件查看算法即可,这里我的是ras加密

这里输入图片描述

actix-web相关代码

上面查出来的算法对应这里用的是rsa_private_keys算法


#[get("/hello/{name}")]
async fn greet(name: web::Path<String>) -> impl Responder {
format!("Hello {name}!")
}

HttpServer::new(move || {
App::new()

.service(greet)


})
// .bind(("127.0.0.1", 8080))?
//服务器用0.0.0.0
//.bind_rustls(("0.0.0.0", 8080), load_rustls_config())?
.bind_rustls(("127.0.0.1", 8080), load_rustls_config())?
.run()
.await
//
fn load_rustls_config() ->ServerConfig {
let key_file = &mut BufReader::new(File::open("certs/key.pem").unwrap());
let key = rsa_private_keys(key_file)
.expect("private key parsing failed")
.into_iter()
.map(PrivateKey)
.next()
.expect("private key file has to contain at least one key");

let cert_file = &mut BufReader::new(File::open("certs/cert.pem").unwrap());
let certificates = certs(cert_file)
.expect("certificate parsing failed")
.into_iter()
.map(Certificate)
.collect();

let config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(certificates, key)
.expect("private key is invalid");
config
}

Cargo.toml配置

actix-web = {version="4.3.1",features = ["rustls"] }
rustls = "0.20.2"
rustls-pemfile = "1"

本地测试

使用postman测试 使用http://127.0.0.1:8080/hello/name http协义访问无响应

这里输入图片描述 使用https://127.0.0.1:8080/hello/name https协义访问有响应

这里输入图片描述