Ruby Webrick HTTP Authentication

2020-05-01 04:22发布

How can I do the same authentication stuff in this page using a subclass like this:

        class Configuration < HTTPServlet::AbstractServlet
        def do_GET (request, response)
            SOMETHING....
        end
        end

    server = HTTPServer.new(:Port => 666)
    server.mount "/conf", Configuration
    trap "INT" do server.shutdown end
    server.start

1条回答
▲ chillily
2楼-- · 2020-05-01 04:25

Seems to work OK for me if you do it in pretty much the same style e.g.

class Configuration < HTTPServlet::AbstractServlet
    def do_GET(req, res)
        HTTPAuth.basic_auth(req, res, "My Realm") {|user, pass|
          # block should return true if
          # authentication token is valid
          user == 'user' && pass == 'topsecret'
        }
        res.body = 
          "Authenticated OK\n"
    end
end

What is the problem you're having?

查看更多
登录 后发表回答