【问题标题】:How to secure my Restful api created in the play framework如何保护我在 play 框架中创建的 Restful api
【发布时间】:2011-12-31 16:26:27
【问题描述】:
我想将用户身份验证添加到我使用基于 Java 的 Play 创建的 restful api!框架。
我目前使用此处的“安全模块”详细信息:http://www.playframework.org/documentation/1.1/secure
当通过http请求直接调用restful api时,我可以使用相同的模块进行身份验证吗?
如果没有,我会很感激一些关于集成另一种身份验证方法的教程/模块的指针(希望可以同时适用于从浏览器访问 Web 应用程序和 http api 访问的方法)。
PS 如果可能的话,最好避免使用 HTTPS 和摘要。
【问题讨论】:
标签:
authentication
rest
playframework
【解决方案1】:
如果您已经在使用用户名/密码机制,为您的 API 调用添加 http 基本身份验证应该不难。您可以创建一个拦截器来执行以下操作:
public class ApiInterceptor extends Controller
{
@Before
static void checkAccess() {
if ( request.user == null || request.password == null) {
unauthorized();
}
boolean canAccess = .... ;// check user and password here
if ( !canAccess ) {
unauthorized();
}
}
}
但这意味着您需要使用 SSL,因为用户名和密码通过 http 以明文形式传输。