JAX-WS实做webservice验证
来源:互联网 发布:织梦 添加视频不播放 编辑:程序博客网 时间:2024/04/29 11:25
在JAX WS标准中,做websevice变得容易,都是用注解等就可以实现了,其中用来做
webservice的权限也是很容易的,比如要根据用户名和密码才能访问ws,下面直接代码,
给出对应的例子,使用的是cxf了.
1 ws接口类
2 WS 接口实现类:
其中,其实就是取出header的信息取进行判断是否有这个权限了,很容易明白.
3 然后是发布这个ws
4 客户端去调用这个WS,其中注意要用户名和密码的传递,这里为了简单,没用证书等了,
只是示意,实际上是复杂多了:
其实核心就是:
headers.put("Username", Collections.singletonList("test"));
headers.put("Password",
Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
webservice的权限也是很容易的,比如要根据用户名和密码才能访问ws,下面直接代码,
给出对应的例子,使用的是cxf了.
1 ws接口类
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- @WebService
- @SOAPBinding(style = Style.RPC)
- public interface HelloWorld {
- @WebMethod
- String getHelloWorldMessage();
- }
2 WS 接口实现类:
- @WebService(endpointInterface = "com.ws.HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- @Resource
- WebServiceContext wsctx;
- @Override
- public String getHelloWorldMessage() {
- MessageContext mctx = wsctx.getMessageContext();
- // 取得报文头
- Map http_headers =
- (Map) mctx.get(
- MessageContext.HTTP_REQUEST_HEADERS);
- List<String> userList = (List) http_headers.get("Username");
- List<String> passList = (List) http_headers.get("Password");
- String username = "";
- String password = "";
- if (userList != null) {
- username = userList.get(0);
- }
- if (passList != null) {
- password = passList.get(0);
- }
- if (username.equals("test")&&password.equals("password")) {
- return "Hello "
- + username +
- " to world of Jax WS - Valid User!";
- } else {
- return " User No Valid!";
- }
- }
- }
其中,其实就是取出header的信息取进行判断是否有这个权限了,很容易明白.
3 然后是发布这个ws
- public static void main(String[] args){
- Endpoint.publish(
- "http://localhost:9000/ws/hello", new HelloWorldImpl());
- }
4 客户端去调用这个WS,其中注意要用户名和密码的传递,这里为了简单,没用证书等了,
只是示意,实际上是复杂多了:
- public class HelloWorldClient {
- private static final String WS_URL =
- "http://localhost:9000/ws/hello?wsdl";
- public static void main(String[] args) throws Exception {
- URL url = new URL(WS_URL);
- QName qname = new QName(
- "http://ws.test.com/",
- "HelloWorldImplService");
- Service service = Service.create(url, qname);
- HelloWorld hello = service.getPort(HelloWorld.class);
- BindingProvider provider = (BindingProvider) hello;
- Map<String, Object> req_ctx = provider.getRequestContext();
- req_ctx.put(
- BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
- //调用的用户名和密码,用map
- Map<String, List<String>> headers = new HashMap<String, List<String>>();
- headers.put("Username", Collections.singletonList("test"));
- headers.put("Password",
- Collections.singletonList("password"));
- req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
- }
- }
其实核心就是:
headers.put("Username", Collections.singletonList("test"));
headers.put("Password",
Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
在消息头中设置好MAP就可以了.
另一个简单例子:http://blog.csdn.net/jackphang/article/details/8788178
0 0
- JAX-WS实做webservice验证
- jax-ws webservice编程
- JAX-WS WebService
- JAX-WS实现WebService
- Jax WS发布webservice
- JAX-WS,WebService实例
- jax-ws webservice编程
- JAX-WS WebService发布
- WebService -- JAX-WS : 入门
- webService之JAX-WS
- jax-ws 处理webservice
- WebService(3):JAX-WS
- WebService JAX-WS 注释
- JAX-WS使用Handler实现简单的WebService权限验证
- JAX-WS使用Handler实现简单的WebService权限验证
- WebService:JAX-WS实现WebService
- 用JAX-WS开发webservice
- Jax-Ws创建WebService服务
- 自定义三档半圆开关控件
- UVA 1252(p287)----Twenty Questions
- path和classpath
- 第四周上机实践项目 项目3--随机函数应用游戏(1)猜数字游戏
- hdoj--Let's go to play(模拟)
- JAX-WS实做webservice验证
- 管闲事的小明
- 【三层】——C#与VB.NET各显神通
- URI数据存储初解
- CodeForces-626A-Robot Sequence
- 最大矩阵和 HDU1081 & NYOJ 104
- Poj 3616 Milking Time【dp】
- c#接口
- ImageView 常用属性的分析