我发现了这篇文章(链接),但它是针对Play 2.0的。
有没有人为Play 1做过这个(我正在使用1.2.4-mbknor-3)?
有没有人为Play 1做过这个(我正在使用1.2.4-mbknor-3)?
Http.Request
对象有从 Authorization 头部填充的 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!");
}
}