Skip to content

Instantly share code, notes, and snippets.

@zaneli
Created March 3, 2012 15:31
Show Gist options
  • Select an option

  • Save zaneli/1966636 to your computer and use it in GitHub Desktop.

Select an option

Save zaneli/1966636 to your computer and use it in GitHub Desktop.
「Axis/Axis2/JAX-WS で Basic/Digest 認証」ブログ用
package com.zaneli.ws.axis2.auth;
import static com.zaneli.ws.axis2.Constants.PASSWORD;
import static com.zaneli.ws.axis2.Constants.USERNAME;
import static org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE;
import static org.apache.axis2.transport.http.HttpTransportProperties.Authenticator.BASIC;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
import com.zaneli.www.GetMessage;
import com.zaneli.www.GetMessageResponse;
import com.zaneli.www.ZaneliWSStub;
public class Axis2AuthExecutor {
public String execute(String text, int num) throws RemoteException {
ZaneliWSStub webService = new ZaneliWSStub();
webService._getServiceClient().getOptions().setProperty(AUTHENTICATE, createAuth(USERNAME, PASSWORD));
GetMessage message = new GetMessage();
message.setText(text);
message.setNum(num);
GetMessageResponse response = webService.getMessage(message);
return response.getGetMessageResult();
}
private Authenticator createAuth(String username, String password) {
List<String> authSchemes = new ArrayList<String>();
// 認証方式はここで設定する。
authSchemes.add(BASIC);
Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setUsername(username);
auth.setPassword(password);
auth.setPreemptiveAuthentication(true);
auth.setAuthSchemes(authSchemes);
return auth;
}
public static void main(String[] args) throws Exception {
System.out.println(new Axis2AuthExecutor().execute("Axis2で実行(Basic 認証)", 111));
}
}
package com.zaneli.ws.axis.auth;
import static com.zaneli.ws.axis.Constants.PASSWORD;
import static com.zaneli.ws.axis.Constants.USERNAME;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.xml.rpc.ServiceException;
import org.apache.axis.AxisEngine;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Stub;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.transport.http.CommonsHTTPSender;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.transport.http.HTTPTransport;
import com.zaneli.www.ZaneliWS;
import com.zaneli.www.ZaneliWSLocator;
import com.zaneli.www.ZaneliWSSoap;
public class AxisAuthExecutor {
public String execute(String text, int num) throws ServiceException, RemoteException {
ZaneliWS service = new ZaneliWSLocator();
// Digest 認証の場合、エンジンを入れ替える必要がある。Basic 認証の場合は不要。
((org.apache.axis.client.Service) service).setEngine(getEngine());
ZaneliWSSoap port = service.getZaneliWSSoap();
setCredential((Stub) port, USERNAME, PASSWORD);
return port.getMessage(text, num);
}
private AxisEngine getEngine() {
CommonsHTTPSender sender = new CommonsHTTPSender();
SimpleProvider clientConfig = new SimpleProvider();
clientConfig.deployTransport(
HTTPTransport.DEFAULT_TRANSPORT_NAME,
new SimpleTargetedChain(sender));
AxisClient engine = new AxisClient(clientConfig);
engine.setOption(HTTPConstants.REQUEST_HEADERS, getChunkedOffHeader());
return engine;
}
private Hashtable<String, Boolean> getChunkedOffHeader() {
Hashtable<String, Boolean> httpHeaders = new Hashtable<String, Boolean>();
httpHeaders.put(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED, false);
return httpHeaders;
}
private void setCredential(Stub stub, String username, String password) {
stub.setUsername(username);
stub.setPassword(password);
}
public static void main(String[] args) throws ServiceException, RemoteException {
System.out.println(new AxisAuthExecutor().execute("Axisで実行(Basic 認証/Digest 認証)", 111));
}
}
package com.zaneli.ws.jaxws.auth;
import static com.zaneli.ws.jaxws.Constants.PASSWORD;
import static com.zaneli.ws.jaxws.Constants.USERNAME;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import com.zaneli.ZaneliWS;
import com.zaneli.ZaneliWSSoap;
public class JaxWSAuthExecutor1 {
public String execute(String text, int num) {
ZaneliWS ws = new ZaneliWS();
ZaneliWSSoap soap = ws.getZaneliWSSoap();
setAuth(((BindingProvider) soap).getRequestContext(), USERNAME, PASSWORD);
return soap.getMessage(text, num);
}
private void setAuth(Map<String, Object> context, String username, String password) {
context.put(BindingProvider.USERNAME_PROPERTY, username);
context.put(BindingProvider.PASSWORD_PROPERTY, password);
}
public static void main(String[] args) {
System.out.println(new JaxWSAuthExecutor1().execute("JAX-WSで実行(Basic 認証)", 111));
}
}
package com.zaneli.ws.jaxws.auth;
import static com.zaneli.ws.jaxws.Constants.PASSWORD;
import static com.zaneli.ws.jaxws.Constants.USERNAME;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import com.zaneli.ZaneliWS;
import com.zaneli.ZaneliWSSoap;
public class JaxWSAuthExecutor2 {
public String execute(String text, int num) {
DigestAuthenticator authenticator = new DigestAuthenticator(USERNAME, PASSWORD);
Authenticator.setDefault(authenticator);
ZaneliWS ws = new ZaneliWS();
ZaneliWSSoap soap = ws.getZaneliWSSoap();
return soap.getMessage(text, num);
}
private static class DigestAuthenticator extends Authenticator {
private final String username;
private final char[] password;
public DigestAuthenticator(final String username, final String password) {
this.username = new String(username);
this.password = password.toCharArray();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(username, password));
}
}
public static void main(String[] args) {
System.out.println(new JaxWSAuthExecutor2().execute("JAX-WSで実行(Basic 認証/Digest 認証)", 111));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment