访问WebSphere上下文之外的LTPA令牌

时间:2013-12-20 19:32:57

标签: java security websphere ltpa

我有一个部署在tomcat中的Web应用程序,从这个Web应用程序中,我必须使用部署在Websphere中的SOAP服务。要访问此服务,我需要传递LTPA令牌。我是websphere的新手,不知道如何在我的网络应用中获得LTPA令牌?我无法修改部署在Web领域的应用程序的实现。

1 个答案:

答案 0 :(得分:0)

我可以通过使用Http Basic Authentication来实现这一点。这是代码段 -

public class TokenHelper {

private static Logger logger = LoggerFactory.getLogger(TokenHelper.class);

private static final int HTTP_TIMEOUT_MILISEC = 100000;
private static String lineSeparator = System.getProperty("line.separator");

@Value("#{'${hostname}'}")
private String hostName;

@Value("#{'${port}'}")
private int port;

@Value("#{'${contextpath}'}")
private String contextPath;

@Value("#{'${isbasicauthentication}'}")
private boolean isBasicAuthentication;

@Value("#{'${username}'}")
private String basicAuthenticationUserName;

@Value("#{'${userpassword}'}")
private String basicAuthenticationPassword;

public Map<String, String> getLtpaToken() throws Exception {    
    Cookie[] cookies = null;
    Protocol protocol = null;
    Map<String, String> cookiesMap = new HashMap<String, String>();

    GetMethod method = new GetMethod();
    HttpClient client = new HttpClient();
    method.getParams().setSoTimeout(HTTP_TIMEOUT_MILISEC);
    protocol = new Protocol("http", new DefaultProtocolSocketFactory(), getPort());

    if (isBasicAuthentication) {
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(getBasicAuthenticationUserName(), getBasicAuthenticationPassword());
        client.getState().setCredentials(new AuthScope(getHostName(), getPort(), AuthScope.ANY_REALM), defaultcreds);
    }
    // Execute request
    try {
        client.getHostConfiguration().setHost(getHostName(), getPort(), protocol);
        method = new GetMethod(getContextPath());
        method.setFollowRedirects(true);

        logger.info(methodName, "URL to get:" + getContextPath());
        // Execute the GET method           
        int statusCode = client.executeMethod(method);
        if (statusCode != -1) {
            cookies = client.getState().getCookies();

            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < cookies.length; j++) {
                cookiesMap.put(cookies[j].getName(), cookies[j].getValue());
                sb.append("CookieName=" + cookies[j].getName() + lineSeparator);
                sb.append("Value=" + cookies[j].getValue() + lineSeparator);
                sb.append("Domain=" + cookies[j].getDomain() + lineSeparator);
            }
            sb.append("Status Text>>>" + HttpStatus.getStatusText(statusCode));
            logger.debug("Cookies are: {}" + sb.toString());

            method.releaseConnection();

        }
    } catch (Exception e) {
        logger.error("Error while getting LTPA token using HttpBasicAuthentication for URL {}" +e);
        throw new RuntimeException("Error while getting LTPA token using HttpBasicAuthentication for URL:" + contextPath, e);
    } finally {
        // Release current connection to the connection pool once you
        // are done
        method.releaseConnection();
    }
    return cookiesMap;
}