Translate
16 Aralık 2019 Pazartesi
WSSAxisHandler & WSSJaxWsHandler
WSSJaxWsHandler.java
package com.pack;
import com.common.enums.Constants;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class WSSJaxWsHandler implements SOAPHandler<SOAPMessageContext> {
private String token;
public WSSJaxWsHandler(String token) {
this.token = token;
}
public boolean handleMessage(SOAPMessageContext context) {
this.addSecurityToken(context);
return true;
}
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
private void addSecurityToken(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean) context.get("javax.xml.ws.handler.message.outbound");
if (outboundProperty) {
Map<String, List<String>> map = (Map) context.get("javax.xml.ws.http.request.headers");
if (map == null) {
map = new HashMap();
context.put("javax.xml.ws.http.request.headers", map);
}
List<String> authTokenList = (List) ((Map) map).get(Constants.SECURE_HEADER);
if (authTokenList == null || authTokenList.isEmpty()) {
synchronized (map) {
authTokenList = (List) ((Map) map).get(Constants.SECURE_HEADER);
if (authTokenList == null || authTokenList.isEmpty()) {
((Map) map).put(Constants.SECURE_HEADER, Arrays.asList((String.format(Constants.SECURE_BEARER + " %s", token))));
}
}
}
}
}
public Set<QName> getHeaders() {
return null;
}
}
WSSAxisHandler.java
package ****.customer.util;
import ****common.config.SpringApplicationContext;
import ****.common.enums.Constants;
import ****security.webservice.WebserviceAuthService;
import javax.xml.namespace.QName;
import javax.xml.rpc.Stub;
import javax.xml.rpc.handler.Handler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.soap.MimeHeaders;
public class WSSAxisHandler implements Handler {
private Stub stub;
private WebserviceAuthService webserviceAuthService;
public WSSAxisHandler() {
}
@Override
public boolean handleRequest(MessageContext messageContext) {
this.addSecurityToken(messageContext);
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext) {
return false;
}
@Override
public boolean handleFault(MessageContext messageContext) {
return false;
}
public void init(HandlerInfo handlerInfo) {
this.stub = (Stub) handlerInfo.getHandlerConfig().get("stub");
}
public void destroy() {
}
public QName[] getHeaders() {
return new QName[0];
}
private void addSecurityToken(MessageContext context) {
if (webserviceAuthService == null) {
webserviceAuthService = SpringApplicationContext.getApplicationContext().getBean(WebserviceAuthService.class);
}
org.apache.axis.MessageContext messageContext = (org.apache.axis.MessageContext) context;
MimeHeaders mimeHeaders = messageContext.getRequestMessage().getMimeHeaders();
String[] headers = mimeHeaders.getHeader(Constants.SECURE_HEADER);
if (headers == null || headers.length == 0) {
synchronized (mimeHeaders) {
headers = mimeHeaders.getHeader(Constants.SECURE_HEADER);
if (headers == null || headers.length == 0) {
String token = webserviceAuthService.getSecureToken();
mimeHeaders.addHeader(Constants.SECURE_HEADER, String.format(Constants.SECURE_BEARER + " %s", token));
}
}
}
}
}
WSSUtil.java
package *****customer.util;
import org.apache.axis.client.Service;
import org.apache.axis.client.Stub;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.HandlerRegistry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WSSUtil {
public static <T extends Stub> void secureAxis(T stub) {
Service service = (Service) stub._getService();
HandlerRegistry handlerRegistry = service.getHandlerRegistry();
synchronized (handlerRegistry) {
List handlerList = handlerRegistry.getHandlerChain(stub.getPortName());
Map<String, Object> config = new HashMap();
config.put("stub", stub);
HandlerInfo info = new HandlerInfo();
info.setHandlerClass(WSSAxisHandler.class);
info.setHandlerConfig(config);
handlerList.add(info);
}
}
}
-----------------------
WSSUtil.secureAxis((Stub) servisssss);
Kaydol:
Kayıt Yorumları (Atom)
Hiç yorum yok:
Yorum Gönder