예제: HTTP 쿠키 검색
다음 예제는 HTTP 요청에서 쿠키를 검색하거나 원본 바이트로 되돌아가도록 쿠키를 디코드하거나 바이트에서 사용자 정의 SingleSignonToken 오브젝트를 작성하는 방법에 대해 보여줍니다. 이 예제는 로그인 모듈에서 해당 단계를 완료하는 방법을 보여줍니다. 그러나 서블릿을 사용하여 해당 단계를 완료할 수도 있습니다.
초기화, 로그인, 커미트 중 수행할 작업에 대한 정보는 JAAS를 위한 시스템 로그인 구성을 위한 사용자 정의 로그인 모듈 개발을 참조하십시오.
public customLoginModule()
{
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map sharedState, Map options)
{
_sharedState = sharedState;
}
public boolean login() throws LoginException
{
// Handles the WSTokenHolderCallback to see if this is an
// initial or propagation login.
Callback callbacks[] = new Callback[2];
callbacks[0] = new WSTokenHolderCallback("Authz Token List: ");
callbacks[1] = new WSServletRequestCallback("HttpServletRequest: ");
try
{
callbackHandler.handle(callbacks);
}
catch (Exception e)
{
// Handles the exception
}
// receive the ArrayList of TokenHolder objects (the serialized tokens)
List authzTokenList = ((WSTokenHolderCallback) callbacks[0]).getTokenHolderList();
javax.servlet.http.HttpServletRequest request =
((WSServletRequestCallback) callbacks[1]).getHttpServletRequest();
if (request != null)
{
// Checks if the cookie is present
javax.servlet.http.Cookie[] cookies = request.getCookies();
String[] cookieStrings = getCookieValues (cookies, "myCookeName1");
if (cookieStrings != null)
{
String cookieVal = null;
for (int n=0;n<cookieStrings.length;n++)
{
cookieVal = cookieStrings[n];
if (cookieVal.length()>0)
{
// Removes the cookie encoding from the cookie to get
// your custom bytes
byte[] cookieBytes =
com.ibm.websphere.security.WSSecurityHelper.
convertCookieStringToBytes(cookieVal);
customSSOToken =
new com.ibm.websphere.security.token.
CustomSingleSignonTokenImpl(cookieBytes);
// Now that you have your cookie from the request,
// you can do something with it here, or add it
// to the Subject in the commit() method for use later.
if (debug || tc.isDebugEnabled())
{
System.out.println("*** GOT MY CUSTOM SSO TOKEN FROM
THE REQUEST ***");
}
}
}
}
}
}
public boolean commit() throws LoginException
{
if (customSSOToken != null)
{
// Sets the customSSOToken token into the Subject
try
{
public final SingleSignonToken customSSOTokenPriv = customSSOToken;
// Do this in a doPrivileged code block so that application code does not
// need to add additional permissions
java.security.AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
try
{
// Add the custom SSO token if it is not null and not
// already in the Subject
if ((customSSOTokenPriv != null) &&
(!subject.getPrivateCredentials().
contains(customSSOTokenPriv)))
{
subject.getPrivateCredentials().add(customSSOTokenPriv);
}
}
catch (Exception e)
{
throw new WSLoginFailedException (e.getMessage(), e);
}
return null;
}
});
}
catch (Exception e)
{
throw new WSLoginFailedException (e.getMessage(), e);
}
}
}
// Private method to get the specific cookie from the request
private String[] getCookieValues (Cookie[] cookies, String hdrName)
{
Vector retValues = new Vector();
int numMatches=0;
if (cookies != null)
{
for (int i = 0; i < cookies.length; ++i)
{
if (hdrName.equals(cookies[i].getName()))
{
retValues.add(cookies[i].getValue());
numMatches++;
System.out.println(cookies[i].getValue());
}
}
}
if (retValues.size()>0)
return (String[]) retValues.toArray(new String[numMatches]);
else
return null;
}
// Defines your login module variables
com.ibm.wsspi.security.token.SingleSignonToken customSSOToken = null;
com.ibm.wsspi.security.token.AuthenticationToken defaultAuthToken = null;
java.util.Map _sharedState = null;
}