在过去的几天里,我一直在努力在Java中创建使用加密的SOAP Web服务。谁能建议我如何实现?
我将向您展示我到目前为止所做的。
1)person.java(pojo class)
package com.journaldev.jaxws.beans;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = -5577579081118070434L;
private String name;
private int age;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString(){
return id+"::"+name+"::"+age;
}
}
2)PersemService(接口)
package com.journaldev.jaxws.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.journaldev.jaxws.beans.Person;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PersonService {
@WebMethod
public boolean addPerson(Person p);
@WebMethod
public boolean deletePerson(int id);
@WebMethod
public Person getPerson(int id);
@WebMethod
public Person[] getAllPersons();
}
3)PersonServiceImpl(服务实施类)
package com.journaldev.jaxws.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jws.WebService;
import com.journaldev.jaxws.beans.Person;
@WebService(endpointInterface = "com.journaldev.jaxws.service.PersonService")
public class PersonServiceImpl implements PersonService {
private static Map<Integer,Person> persons = new HashMap<Integer,Person>();
@Override
public boolean addPerson(Person p) {
if(persons.get(p.getId()) != null) return false;
persons.put(p.getId(), p);
return true;
}
@Override
public boolean deletePerson(int id) {
if(persons.get(id) == null) return false;
persons.remove(id);
return true;
}
@Override
public Person getPerson(int id) {
return persons.get(id);
}
@Override
public Person[] getAllPersons() {
Set<Integer> ids = persons.keySet();
Person[] p = new Person[ids.size()];
int i=0;
for(Integer id : ids){
p[i] = persons.get(id);
i++;
}
return p;
}
}
4)Soappublisher(发布者或服务器)
软件包com.journaldev.jaxws.service;
import javax.xml.ws.Endpoint;
public class SOAPPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/ws/person", new PersonServiceImpl());
}
}
5)soappublisherclient(client)
package com.journaldev.jaxws.service;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.journaldev.jaxws.beans.Person;
public class SOAPPublisherClient {
public static void main(String[] args) throws MalformedURLException {
URL wsdlURL = new URL("http://localhost:8080/ws/person?wsdl");
//check above URL in browser, you should see WSDL file
//creating QName using targetNamespace and name
QName qname = new QName("http://service.jaxws.journaldev.com/", "PersonServiceImplService");
Service service = Service.create(wsdlURL, qname);
//We need to pass interface and model beans to client
PersonService ps = service.getPort(PersonService.class);
Person p1 = new Person(); p1.setName("Pankaj"); p1.setId(1); p1.setAge(30);
Person p2 = new Person(); p2.setName("Meghna"); p2.setId(2); p2.setAge(25);
//add person
System.out.println("Add Person Status="+ps.addPerson(p1));
System.out.println("Add Person Status="+ps.addPerson(p2));
//get person
System.out.println(ps.getPerson(1));
//get all persons
System.out.println(Arrays.asList(ps.getAllPersons()));
//delete person
System.out.println("Delete Person Status="+ps.deletePerson(2));
//get all persons
System.out.println(Arrays.asList(ps.getAllPersons()));
}
}
这是我目前所做的。我想知道如何使用X.509加密消息主体。对于服务器和客户端代码,任何人都可以帮助我。
有一个被良好的肥皂扩展名,称为WS安全性。该标准定义了签名和加密消息的外观。
我不确定您使用的是哪个框架(CXF,AXIS2,其他?)每个框架都有不同的方法来设置消息加密。
对于cxf,我建议从本读物开始,甚至有一些链接。
我无法从以前的项目中找到/链接代码片段,如果找到它们,我会在允许的时候尝试在这里粘贴它们。