如何在Play Framework 1.2中实现HTTP Basic Auth?

3
我发现了这篇文章(链接),但它是针对Play 2.0的。
有没有人为Play 1做过这个(我正在使用1.2.4-mbknor-3)?
1个回答

6
Http.Request 对象有从 Authorization 头部填充的 userpassword 属性。您可以像这样做:
public class Application extends Controller {   
  private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
  private static final String REALM = "Basic realm=\"Your Realm Here\"";

  @Before
  static void authenticate() {
    if (!("username".equals(request.user) && "password".equals(request.password))) {
      response.setHeader(WWW_AUTHENTICATE, REALM);
      error(401, "Unauthorized");
    }
  }

  public static void index() {
    renderText("Welcome!");
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接