ripper234
ripper234

Reputation: 230048

How to implement HTTP Basic Auth in Play Framework 1.2?

I found this post, but it is aimed at Play 2.0.

Has anyone done this for Play 1 (I'm using 1.2.4-mbknor-3)?

Upvotes: 3

Views: 2479

Answers (1)

The Http.Request object has user and password properties populated from the Authorization header. You could do something like this:

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!");
  }
}

Upvotes: 6

Related Questions