I found this post,但它的目标是Play 2.0。
有没有人为Play 1做过这个(我使用的是1.2.4-mbknor-3)?
答案 0 :(得分:6)
Http.Request
对象的授权标头中填充了user
和password
个属性。你可以这样做:
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!");
}
}