使用JAX-WS进行应用程序身份验证「建议收藏」

使用JAX-WS进行应用程序身份验证「建议收藏」在JAX-WS中处理身份验证的常用方法之一是客户端提供“用户名”和“密码”,将其附加在SOAP请求标头中并发送到服务器,服务器解析SOAP文档并检索提供的“用户名”和“密码”从请求标头中进行,并从数据库中进行验证,或者使用其他任何方法。在本文中,我们向您展示如何实现上述“JAX-WS中的应用程序级别认证”。想法…在Web服务客户端站点上,只需将“用户名”和“密码”放入请…

大家好,又见面了,我是你们的朋友全栈君。

在JAX-WS中处理身份验证的常用方法之一是客户端提供“用户名”和“密码”,将其附加在SOAP请求标头中并发送到服务器,服务器解析SOAP文档并检索提供的“用户名”和“密码”从请求标头中进行,并从数据库中进行验证,或者使用其他任何方法。

在本文中,我们向您展示如何实现上述“ JAX-WS中的应用程序级别认证 ”。

想法…

Web服务客户端站点上,只需将“用户名”和“密码”放入请求标头即可。

Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
    req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("mkyong"));
    headers.put("Password", Collections.singletonList("password"));
    req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

Web服务服务器站点上,通过WebServiceContext获取请求标头参数。

@Resource
    WebServiceContext wsctx;

    @Override
    public String method() {
		
        MessageContext mctx = wsctx.getMessageContext();
		
	//get detail from request headers
        Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
        List userList = (List) http_headers.get("Username");
        List passList = (List) http_headers.get("Password");

    //...

仅此而已,已部署的JAX-WS受支持的应用程序级别身份验证。

使用JAX-WS认证示例

查看完整示例。

1. WebService服务器

创建一个简单的JAX-WS hello world示例,以处理应用程序级别的身份验证。

文件:HelloWorld.java

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
	
	@WebMethod String getHelloWorldAsString();
	
}

HelloWorldImpl.java

package com.mkyong.ws;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

//Service Implementation Bean
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Resource
    WebServiceContext wsctx;

    @Override
    public String getHelloWorldAsString() {
		
	MessageContext mctx = wsctx.getMessageContext();
		
	//get detail from request headers
        Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
        List userList = (List) http_headers.get("Username");
        List passList = (List) http_headers.get("Password");

        String username = "";
        String password = "";
        
        if(userList!=null){
        	//get username
        	username = userList.get(0).toString();
        }
        	
        if(passList!=null){
        	//get password
        	password = passList.get(0).toString();
        }
        	
        //Should validate username and password with database
        if (username.equals("mkyong") && password.equals("password")){
        	return "Hello World JAX-WS - Valid User!";
        }else{
        	return "Unknown User!";
        }
       
    }	
}

2. EndPoint Publisher

创建一个端点发布程序以通过以下URL在Web服务之上进行部署:“ http:// localhost:9999 / ws / hello

文件:HelloWorldPublisher.java

package com.mkyong.endpoint;
 
import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
 
//Endpoint publisher
public class HelloWorldPublisher{
 
    public static void main(String[] args) {
	   Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
    }
 
}

3. WebService客户端

创建一个Web服务客户端,以发送“用户名”和“密码”进行身份验证。

文件:HelloWorldClient.java

package com.mkyong.client;

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;

import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{
	
	private static final String WS_URL = "http://localhost:9999/ws/hello?wsdl";
		
	public static void main(String[] args) throws Exception {
	   
	URL url = new URL(WS_URL);
        QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        
        /*******************UserName & Password ******************************/
        Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList("mkyong"));
        headers.put("Password", Collections.singletonList("password"));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        /**********************************************************************/
        
        System.out.println(hello.getHelloWorldAsString());
       
    }
}

输出量

Hello World JAX-WS - Valid User!

4.跟踪SOAP流量

从上到下,显示SOAP信封如何在客户端和服务器之间流动。

1.客户端发送请求,用户名“ mkyong ”和密码“ password ”包含在SOAP信封中。

POST /ws/hello?wsdl HTTP/1.1
Password: password
Username: mkyong
SOAPAction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
User-Agent: Java/1.6.0_13
Host: localhost:8888
Connection: keep-alive
Content-Length: 178

<?xml version="1.0" ?>
	<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
		<S:Body>
			<ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/"/>
		</S:Body>
	</S:Envelope>

2.服务器发回正常响应。

HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8

<?xml version="1.0" ?>
	<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
		<S:Body>
			<ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/">
				<return>Hello World JAX-WS - Valid User!</return>
			</ns2:getHelloWorldAsStringResponse>
		</S:Body>
	</S:Envelope>

做完了

下载源代码

下载它– JAX-WS-Application-Authentication-Example.zip (10KB)

翻译自: https://mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159711.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • windows修改dns服务器,windowns中dns服务器配置与管理详解(多图)

    windows修改dns服务器,windowns中dns服务器配置与管理详解(多图)安装DNS服务器在”服务器管理器”-“角色”-“添加角色”中安装DNS服务器。选择DNS服务器点下一步安装,然后安装固定服务器IP地址安装完DNS和配置好固定ip后,我们就可以开始配置DNS。在”开始”-“管理工具”-DNS打开DNS正向区域的建立并为其设置主机。选择”正向查找区域”-右击-新建区域。填写域名之后全部默认设置,直至完成。域名建立之后,我们就可以为其”新建主机”选择”hzu.com”…

    2022年6月3日
    128
  • excel查找结果导出_excel数据怎么导出

    excel查找结果导出_excel数据怎么导出使用PhpSpreadsheet导入&amp;amp;导出Excel,通用方法。适用各种Excel操作场景!

    2025年12月14日
    3
  • phptorm 激活码【在线注册码/序列号/破解码】

    phptorm 激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    38
  • mysql数据库高可用方案_MySQL集群方案

    mysql数据库高可用方案_MySQL集群方案在分布式系统中,我们往往会考虑系统的高可用,对于无状态程序来讲,高可用实施相对简单一些,纵向、横向扩展起来相对容易,然而对于数据密集型应用,像数据库的高可用,就不太好扩展。我们在考虑数据库高可用时,主要考虑发生系统宕机意外中断的时候,尽可能的保持数据库的可用性,保证业务不会被影响;其次是备份库,只读副本节点需要与主节点保持数据实时一致,当数据库切换后,应当保持数据的一致性,不会存在数据缺失或者数据…

    2025年7月15日
    4
  • webstorm必装十大插件_webpack常用插件

    webstorm必装十大插件_webpack常用插件activate-power-mode狂拽炫酷吊炸天装逼的插件,atom上的神器啊,抱着试一试的心态一搜,webstorm上居然也有了,安装之后可以在window->activate-power-mode中关闭震动以及开启彩色模式。TabNine可以记录用户习惯自动补全代码,牛逼ESLint代码检查插件RainbowBrackets彩虹色的括号,颜……

    2025年10月13日
    3
  • idea配置运行springboot项目_java项目框架搭建流程

    idea配置运行springboot项目_java项目框架搭建流程IDEA创建springboot项目

    2025年10月31日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号