服务端
因为自己调用的服务,没有做用户密码校验可能是不安全的,所以做一个简单的校验
/** * @author WGR * @create 2020/3/10 -- 22:47 */ public class LoginCheck { /** * 〈一句话功能简述〉 〈功能详细描述〉 * * @throws AxisFault */ public static void checkUserPwd() throws AxisFault { MessageContext msgContext = MessageContext.getCurrentMessageContext(); // 获取Head Iterator list = (Iterator) msgContext.getEnvelope().getHeader() .getFirstElement().getChildren(); String Username = ""; String Password = ""; while (list.hasNext()) { OMElement element = (OMElement) list.next(); if (element.getLocalName().equals("Username")) { Username = element.getText(); } if (element.getLocalName().equals("Password")) { Password = element.getText(); } } if (!Username.equals("toone") || !Password.equals("111111")) { throw new AxisFault( " Authentication Fail! Check username/password "); } } }
加在下面这个方法上
public class HelloServiceImpl implements HelloService { @Override public String sayHello(String info) throws AxisFault { LoginCheck.checkUserPwd(); return "sayHello:"+info; } }
客户端
客户端也不难,就是往SOAPHeader中添加相应字段就行,网上很多代码可以参考。
这里是我用到的代码:
public class HeaderOMElement { public static OMElement createHeaderOMElement() { OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace SecurityElementNamespace = factory.createOMNamespace("http://impl.service", "wsse"); OMElement authenticationOM = factory.createOMElement("Authentication", SecurityElementNamespace); OMElement usernameOM = factory.createOMElement("Username", SecurityElementNamespace); OMElement passwordOM = factory.createOMElement("Password", SecurityElementNamespace); usernameOM.setText("toone"); passwordOM.setText("22222"); authenticationOM.addChild(usernameOM); authenticationOM.addChild(passwordOM); return authenticationOM; } }
然后在需要调用服务的时候,将创建得到的OMElement附加上去就行了。
这里也给出代码参考: