dpql57753 2014-06-12 09:36
浏览 237
已采纳

申请客户端证书进行身份验证

I would like request a certificate to the browser for authenticate members.

In nodejs we have something like http://nategood.com/nodejs-ssl-client-cert-auth-api-rest

I have read some articles about tls, but I don't really understand how use it...

  • 写回答

1条回答 默认 最新

  • douxun7992 2014-06-12 11:07
    关注

    Here is a short example of how to require client certificate. The trick is to manually create and configure the http.Server instead of using the utilitary routines.

    package main
    
    import (
        "crypto/tls"
        "fmt"
        "net/http"
    )
    
    func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello cert")
        })
    
        server := &http.Server{
            Addr: ":8090",
            TLSConfig: &tls.Config{
                ClientAuth: tls.RequireAndVerifyClientCert,
            },
        }
    
        server.ListenAndServeTLS("cert.pem", "cert.key")
    }
    

    The important part is the tls.Config struct which control the way the server will behave with TLS. The field ClientAuth hold the client certificate policy, in our case Require a client certificate and verify it. Note that other policies are available…

    You should also have a look at the ClientCAs field of the same struct, that allow you to use a list of root CA the client must verify against.

    Note: I assume that you are also using a certificate server side to encrypt the communication. The server.ListenAndServeTLS method still do a lot of the work for you as a side-effect. If you don't need it, you will have to dive into this method to do it manually (and use the even-lower-level method server.Serve).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!