我创建了基于Jersey的Web服务(通过Netbeans自动生成).
我还创建了一个用户名"testClient",密码为"secret",并创建了用户组"Users",并使用了glassfish 3.0.1管理控制台使用文件Realm.
我还相应地映射了web.xml和sun-web.xml.
我的网络服务已成功保护; 当我访问该网站时,我收到安全警告,然后我提示用户名和密码来访问该网站的任何内容.通过Web浏览器访问它时工作正常.
现在我已经编写了一个基于jersey的简单客户端,并试图访问第一个项目提供的Web服务; 客户端代码在这里
自动生成Jersey客户端代码
public class JerseyClient {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "https://localhost:9028/testsecurity2/resources";
public JerseyClient() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig(); // SSL configuration
// SSL configuration
config.getProperties().put(com.sun.jersey.client.urlconnection.HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new com.sun.jersey.client.urlconnection.HTTPSProperties(getHostnameVerifier(), getSSLContext()));
client = Client.create(config);
webResource = client.resource(BASE_URI).path("manufacturers");
}
public <T> T get_XML(Class<T> responseType) throws UniformInterfaceException {
return webResource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T get_JSON(Class<T> responseType) throws UniformInterfaceException {
return webResource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void close() {
client.destroy();
}
public …Run Code Online (Sandbox Code Playgroud)