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

时间:2013-01-16 10:45:33

标签: playframework playframework-1.x http-basic-authentication

I found this post,但它的目标是Play 2.0。

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

1 个答案:

答案 0 :(得分: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!");
  }
}