所以问题是:我在 Jersey 开发了在 Glassfish 上运行的 REST 服务。对于身份验证,我实现 了 Basic-Authentication。在客户端,我通过ApacheHTTPClient实现了身份验证。
我的想法只是在注册用户进入时要求身份验证 - 比如登录。是在客户端应用程序中配置的(在用户注销之前保持身份验证有效),还是在我配置基本身份验证的 REST 服务中配置?
谢谢!
这就是我在客户端应用程序上进行登录的方式:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
public class UserLogin {
private static final String BASE_URI = "http://localhost:8080/LULServices/webresources";
public static void main(String[] args) throws Exception {
final DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("zzzzz", "xxxxx"));
HttpPut httpPut = new HttpPut(BASE_URI + "/services.users/login");
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
httpPut.addHeader("Content-type", "multipart/form-data");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", "zzzzz"));
nameValuePairs.add(new BasicNameValuePair("password","xxxxx"));
httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPut);
try {
System.out.println("Executing request " + httpPut.getRequestLine());
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println("HTTP Status: " + response.getStatusLine());
String putResponse = EntityUtils.toString(entity);
System.out.println(putResponse);
EntityUtils.consume(entity);
} finally
httpPut.releaseConnection();
} finally
httpclient.getConnectionManager().shutdown();
}
}
它返回给用户他的secret_id。