过去几次,我不得不在WebSphere Application Server上运行的应用程序中建立经过身份验证的HTTP或HTTPS连接。
我通常创建了一些自定义的方式来存储连接的用户名和密码-使用文本或XML文件,或者进行测试,仅进行硬编码-并将这些详细信息添加到HttpURLConnection中的Authorization标头中,以执行HTTP Basic身份验证。
我最近认为WAS集成解决方案控制台提供了一种配置用户名和密码的方式(在“安全性”->“ Java身份验证和授权服务”->“ J2C身份验证数据”下),并且使用内置功能(如果可能)是有意义的),而不是总是以自定义的方式执行此操作,但是经过一个小时左右的搜索,我一直无法弄清楚该如何执行。
那么,有谁知道使用配置的WAS J2C认证数据来认证任意HTTP或HTTPS连接是否可行,以及如何做到这一点?
我假设它需要WebSphere API才能:
另外:如果Apache HttpClient可以简化此操作,我可以使用它,但是仍然需要适当的WebSphere API。
我发现了一个可能对WebSphere Portal(com.ibm.wps.portletservice.credentialvault.credentials.LtpaTokenCredential
)有用的类,但是我需要在非Portal环境中执行此操作,因此我不能使用任何特定于Portal的类或接口。
我还需要至少在以下类型的应用程序中工作:Servlet和WebSphere Trust Association Interceptor。
我只是在servlet中尝试了以下内容(从此处进行修改),它可以抓取auth别名user / pwd。
import com.ibm.websphere.security.NotImplementedException;
import com.ibm.wsspi.security.auth.callback.Constants;
import com.ibm.wsspi.security.auth.callback.WSMappingCallbackHandlerFactory;
import javax.resource.spi.security.PasswordCredential;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
Map map = new HashMap();
map.put(Constants.MAPPING_ALIAS, "REPLACE_WITH_YOUR_AUTH_ALIAS");
CallbackHandler callbackHandler = null;
try {
callbackHandler = WSMappingCallbackHandlerFactory.getInstance().getCallbackHandler(map, null);
} catch (NotImplementedException e) {
}
LoginContext loginContext = null;
try {
loginContext = new LoginContext("DefaultPrincipalMapping", callbackHandler);
loginContext.login();
} catch (LoginException e) {
}
Subject subject = loginContext.getSubject();
Set credentials = subject.getPrivateCredentials();
PasswordCredential passwordCredential = (PasswordCredential) credentials.iterator().next();
String user = passwordCredential.getUserName();
String password = new String(passwordCredential.getPassword());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>User: " + user + " --- Password: " + password+"</h1>");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句