如果您與AppEninge的UserService集成(並且與Google帳戶同樣),則可以阻止任何查詢。 RESTlet擁有一個超級優雅的身份驗證程序,它附帶框架:
public class GaeAuthenticator extends Authenticator {
/**
* The GAE UserService that provides facilities to check whether a user has
* authenticated using their Google Account
*/
private UserService userService = UserServiceFactory.getUserService();
/**
* Constructor setting the mode to "required".
*
* @param context
* The context.
* @see #Authenticator(Context)
*/
public GaeAuthenticator(Context context) {
super(context);
}
/**
* Constructor using the context's default enroler.
*
* @param context
* The context.
* @param optional
* The authentication mode.
* @see #Authenticator(Context, boolean, Enroler)
*/
public GaeAuthenticator(Context context, boolean optional) {
super(context, optional);
}
/**
* Constructor.
*
* @param context
* The context.
* @param optional
* The authentication mode.
* @param enroler
* The enroler to invoke upon successful authentication.
*/
public GaeAuthenticator(Context context, boolean optional, Enroler enroler) {
super(context, optional, enroler);
}
/**
* Integrates with Google App Engine UserService to redirect
* non-authenticated users to the GAE login URL. Upon successful login,
* creates a Restlet User object based values in GAE user object. The GAE
* "nickname" property gets mapped to the Restlet "firstName" property.
*
* @param request
* The request sent.
* @param response
* The response to update.
* @return True if the authentication succeeded.
*/
@Override
protected boolean authenticate(Request request, Response response) {
ClientInfo info = request.getClientInfo();
if (info.isAuthenticated()) {
// The request is already authenticated.
return true;
} else if (userService.isUserLoggedIn()) {
// The user is logged in, create restlet user.
com.google.appengine.api.users.User gaeUser = userService
.getCurrentUser();
User restletUser = new User(gaeUser.getUserId());
restletUser.setEmail(gaeUser.getEmail());
restletUser.setFirstName(gaeUser.getNickname());
info.setUser(restletUser);
info.setAuthenticated(true);
return true;
} else {
// The GAE user service says user not logged in, let's redirect him
// to the login page.
String loginUrl = userService.createLoginURL(request
.getOriginalRef().toString());
response.redirectTemporary(loginUrl);
return false;
}
}
}
有一個叫做緩存的新東西,您可能想研究它! – 2012-01-18 21:02:16
如果您的憑證存儲在文件中,它並不意味着數據庫請求:-) – cmbuckley 2012-01-18 21:09:46
@cbuckley文件請求可能比數據庫請求慢,所以我認爲它不是一個選項。 – miguelcobain 2012-01-18 21:56:09