如何为独立的 Sinatra 应用程序启用 SSL?

发布于 2024-08-23 02:22:07 字数 137 浏览 10 评论 0原文

我想在 Sinatra 中编写一个快速的服务器应用程序。它必须是独立的(即不使用 apache/nginx/passenger),但也必须支持 SSL。

有没有一种简单的方法来启用 Sinatra 的 SSL 支持(例如使用 WEBRick)?

I want to write a quick server app in Sinatra. It has to be self-contained (i.e. not use apache/nginx/passenger) but also has to support SSL.

Is there an easy way to enable SSL support for Sinatra (using WEBRick for example)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2024-08-30 02:22:07

要使用 MRI ruby​​ 执行此操作,请使用以下猴子补丁:

sinatra_ssl.rb

require 'webrick/https'

module Sinatra
  class Application
    def self.run!
      certificate_content = File.open(ssl_certificate).read
      key_content = File.open(ssl_key).read

      server_options = {
        :Host => bind,
        :Port => port,
        :SSLEnable => true,
        :SSLCertificate => OpenSSL::X509::Certificate.new(certificate_content),
        :SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content)
      }

      Rack::Handler::WEBrick.run self, server_options do |server|
        [:INT, :TERM].each { |sig| trap(sig) { server.stop } }
        server.threaded = settings.threaded if server.respond_to? :threaded=
        set :running, true
      end
    end
  end
end

然后,在您的独立应用程序中:

app.rb

require 'sinatra'
require 'sinatra_ssl'

set :port, 8443
set :ssl_certificate, "server.crt"
set :ssl_key, "server.key"

get "/" do
  "Hello world!"
end

To do this with MRI ruby, use the following monkeypatch:

sinatra_ssl.rb:

require 'webrick/https'

module Sinatra
  class Application
    def self.run!
      certificate_content = File.open(ssl_certificate).read
      key_content = File.open(ssl_key).read

      server_options = {
        :Host => bind,
        :Port => port,
        :SSLEnable => true,
        :SSLCertificate => OpenSSL::X509::Certificate.new(certificate_content),
        :SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content)
      }

      Rack::Handler::WEBrick.run self, server_options do |server|
        [:INT, :TERM].each { |sig| trap(sig) { server.stop } }
        server.threaded = settings.threaded if server.respond_to? :threaded=
        set :running, true
      end
    end
  end
end

Then, in your standalone application:

app.rb

require 'sinatra'
require 'sinatra_ssl'

set :port, 8443
set :ssl_certificate, "server.crt"
set :ssl_key, "server.key"

get "/" do
  "Hello world!"
end
晨敛清荷 2024-08-30 02:22:07

使用 JRuby 解释器 + jetty-rackup gem (http://github.com/geekq/jetty-rackup)
在jetty-rackup gem中编辑jetty-rackup文件并添加一个SslSocketConnector,一些代码可以帮助您:

    security_connector = Jetty::Security::SslSocketConnector.new
    security_connector.set_acceptors(config[:acceptor_size])
    security_connector.port = config[:port]
    security_connector.confidential_port = config[:port]
    security_connector.keystore = keystore
    security_connector.password = config[:password]
    security_connector.key_password = config[:key_password].nil? ? config[:password] : config[:key_password]
    security_connector.truststore = truststore
    security_connector.trust_password = config[:trust_pasword].nil? ? config[:password] : config[:trust_pasword]
    server.add_connector(security_connector)

示例配置:

# Config
:acceptor_size: 10
:ssl: true
:keystore: keystore.jks
:password: your_pass
# :key_password: your_pass # if different
# :truststore: truststore.jks # if different
# :trust_pasword: your_pass # if different

生成keystore.jks:http://docs.codehaus.org/display/JETTY/How+to+configure+SSL

Use JRuby interpreter + jetty-rackup gem (http://github.com/geekq/jetty-rackup)
Edit jetty-rackup file in the jetty-rackup gem and add a SslSocketConnector, some code to help you:

    security_connector = Jetty::Security::SslSocketConnector.new
    security_connector.set_acceptors(config[:acceptor_size])
    security_connector.port = config[:port]
    security_connector.confidential_port = config[:port]
    security_connector.keystore = keystore
    security_connector.password = config[:password]
    security_connector.key_password = config[:key_password].nil? ? config[:password] : config[:key_password]
    security_connector.truststore = truststore
    security_connector.trust_password = config[:trust_pasword].nil? ? config[:password] : config[:trust_pasword]
    server.add_connector(security_connector)

Sample config:

# Config
:acceptor_size: 10
:ssl: true
:keystore: keystore.jks
:password: your_pass
# :key_password: your_pass # if different
# :truststore: truststore.jks # if different
# :trust_pasword: your_pass # if different

Generating keystore.jks : http://docs.codehaus.org/display/JETTY/How+to+configure+SSL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文