SOAP Web Service: Authentication Trong JAX-WS - GP Coder

Một trong những cách được sử dụng để chứng thực (Authentication) người dùng trong JAX-WS là Client sẽ cung cấp username/ password trong SOAP request header và gửi lên server. Server sẽ parse SOAP document và lấy thông tin username/ password từ request header và sau đó thực hiện truy xuất database để validate hoặc làm bất kỳ cái gì nếu muốn.

Trong bài này, chúng ta sẽ cùng tìm hiểu cách chứng thực Client trong JAX-WS.

Nội dung

  • 1 Tạo WebService Server
  • 2 Tạo Client truy cập WS

Tạo WebService Server

WelcomeService.java

package com.gpcoder.ws.authentication; 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 WelcomeService { @WebMethod String getWelcomeMsg(String name); }

Chúng ta sử dụng WebServiceContext để truy xuất thông tin được Client gửi lên trong header.

WelcomeServiceImpl.java

package com.gpcoder.ws.authentication; 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; @WebService(endpointInterface = "com.gpcoder.ws.authentication.WelcomeService") public class WelcomeServiceImpl implements WelcomeService { @Resource private WebServiceContext wsctx; @Override public String getWelcomeMsg(String name) { MessageContext mctx = wsctx.getMessageContext(); // get detail from request headers Map<String, Object> headers = (Map<String, Object>) mctx.get(MessageContext.HTTP_REQUEST_HEADERS); List<String> users = (List<String>) headers.get("username"); List<String> pwds = (List<String>) headers.get("password"); if (users != null && pwds != null) { if ("gpcoder".equals(users.get(0)) && "jax-ws-by-gpcoder".equals(pwds.get(0))) { return "Welcome " + name; } else { return "Authentication failed!"; } } return "Username and password are not provided!"; } }

WelcomePublisher.java

package com.gpcoder.ws.authentication; import javax.xml.ws.Endpoint; public class WelcomePublisher { public static final String WS_URL = "http://localhost:8080/ws/welcome"; public static void main(String[] args) { Endpoint.publish(WS_URL, new WelcomeServiceImpl()); } }

Tạo Client truy cập WS

Chúng ta sử dụng RequestContext để gửi thông tin username và password lên Server. Lưu ý: giá trị trong header là kiểu Map<String, List<Object>>.

WelcomeClient.java

package com.gpcoder.ws.authentication; import java.net.MalformedURLException; 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; public class WelcomeClient { private static final String WS_URL = WelcomePublisher.WS_URL + "?wsdl"; public static void main(String[] args) throws MalformedURLException { // Create URL of .wsdl file URL wsdlURL = new URL(WS_URL); // Create a QName using targetNamespace and name QName qname = new QName("http://authentication.ws.gpcoder.com/", "WelcomeServiceImplService"); // Creates a Service instance with the specified WSDL document location and // service qualified name Service service = Service.create(wsdlURL, qname); // We need to pass interface and model beans to client WelcomeService userService = service.getPort(WelcomeService.class); // Prepare username & password Map<String, List<String>> headers = new HashMap<>(); headers.put("username", Collections.singletonList("gpcoder")); headers.put("password", Collections.singletonList("jax-ws-by-gpcoder")); // Set request to header Map<String, Object> requestContext = ((BindingProvider) userService).getRequestContext(); requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); // Send request and receive response System.out.println("Result: " + userService.getWelcomeMsg("gpcoder")); } }

Chạy file WelcomePublisher.java trước, sau đó chạy file WelcomeClient.java chúng ta có kết quả như sau:

Result: Welcome gpcoder 5.008 Nếu bạn thấy hay thì hãy chia sẻ bài viết cho mọi người nhé! Và Donate tác giảShares

Chuyên mục: Java Webservice, SOAP Được gắn thẻ: Authentication, SOAP, Webservice

Giới thiệu SOAP UI và thực hiện test Web ServiceSOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS

Có thể bạn muốn xem:

  • Rest Web service: Filter và Interceptor với Jersey 2.x (P1) (25/06/2019)
  • Test REST Web Service đơn giản hơn với REST Assured (26/08/2019)
  • SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS (06/06/2019)
  • REST Web service: JWT – Token-based Authentication trong Jersey 2.x (10/07/2019)
  • REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x (21/06/2019)

Bình luận

bình luận

Từ khóa » Jax-ws Là Gì