回答

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