Restful Java - 授予对资源的访问权限

发布于 2024-11-05 21:38:48 字数 306 浏览 0 评论 0原文

我开发了一个资源丰富的网络服务,它将拥有不同的资源(应用程序数据库存储用户名和密码)。

另一方面,我有一个可以访问这些资源的客户端。要访问其中一些资源,客户端必须注册(存在于数据库中)。我使用 GAE 作为应用程序服务器和 Jersey 来创建 Restful WS,所以我的问题是访问此资源的最佳方式是什么?

我想在 WS 请求(作为 POST)中发送用户名和密码,然后检查数据库中是否存在这对夫妇并回答请求。

您认为这种身份验证安全吗(我认为我不能在 GAE 上使用 https)?

预先感谢

Danilo 的回复

I developing a resful webservice that will have differentes resources (the application database stores urename and password).

On the other and I have a client that can access to those resources. To access to some of these resources the client must be registered (existing in the database). Im using GAE as application server and Jersey to create the Restful WS, so my question is what is the best way to access this resources?

I thought in sending the username and password in the WS request (as POST) and then check if the couple exists in the db and answering at the request.

Do you think this kind of authentication is secure (I dont think I can use https on GAE)?

Thanks in advance fo the responces

Danilo

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

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

发布评论

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

评论(2

我一直都在从未离去 2024-11-12 21:38:48

您应该使用 OAuth 1.0 或 2.0

You should be using OAuth 1.0 or 2.0

云巢 2024-11-12 21:38:48

您可能希望使用某种形式的传输层安全性(例如 SSL)来保护您的服务。

此外,REST 尝试利用 HTTP 功能,因此您可以在 HTTP 请求中添加授权标头。

编辑:一个简单的示例

传输层安全
前往销售 SSL 证书的众多公司之一,例如 Verisign。购买证书。将其安装在您的网络服务器上。您的网络服务器将有有关如何安装它的文档。

使用HTTP授权
让 Web 服务的客户端使用 BASIC 授权(它们以明文形式传递,因此您必须使用 SSL 才能生效)。这涉及将 BASE64 编码的用户名和密码放入授权标头中。编写安全过滤器并配置您的网络应用以通过过滤器将所有请求传递到 RESTful 服务。您的安全过滤器应从请求授权标头中提取用户名和密码并检查它们。如果凭据无效,您将使用 403 状态代码拒绝请求。如果凭据正常,则只需向上传播过滤器链即可。

public MySecurityFilter implements Filter {

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {

    //Get the Authorizatiuon header
    String auth = ((HttpServletRequest)request).getHeader("Authorization");

    //Extract the username and password

    if (checkCredentialsOnMyDatabase(credentials[0], credentials[1])) {
      chain.doFilter(request, response);
    } else {
      //Reject the request with status code 403
    }
  }
}

另请注意,如果您使用的是 Jersey 等流行框架之一,那么它内置的许多安全功能可能会对您有所帮助。

You might want to use some form of transport layer security such as SSL to secure your service.

Also, REST tries to make use of HTTP features, so you could put a Authorization header in the HTTP request.

Edit: a simple example

Transport Layer Security
Go to one of the many companies that sell SSL certificates, for example Verisign. Buy a certificate. Install it on your web server. You web server will have documentation on how to install it.

Using HTTP authorisation
Get the client of your web service to use BASIC authorization (they are passed in plaintext so you've got to be using SSL for this to be effective). This involves putting the BASE64 encoded username and password in the Authorisation header. Write a security Filter and configure your web app to pass all requests to your RESTful service through the filter. Your security filter should extract the username and password from the request Authorisation header and check them. If the credentials are invalid you reject the request with a 403 status code. If the credentials are OK, just propogate up the filter chain.

public MySecurityFilter implements Filter {

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {

    //Get the Authorizatiuon header
    String auth = ((HttpServletRequest)request).getHeader("Authorization");

    //Extract the username and password

    if (checkCredentialsOnMyDatabase(credentials[0], credentials[1])) {
      chain.doFilter(request, response);
    } else {
      //Reject the request with status code 403
    }
  }
}

Also note that if you are using one of the popular frameworks like Jersey then it has a lot of security functions built in that may help you.

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