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

rip*_*234 3 basic-authentication playframework playframework-1.x

我找到了这篇文章,但它的目标是Play 2.0.

有没有人为Play 1做过这个(我使用的是1.2.4-mbknor-3)?

Wer*_*rås 6

Http.Request对象具有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!");
  }
}
Run Code Online (Sandbox Code Playgroud)