Read LTPA token with API

本文介绍了两种获取LtpaToken的方法:通过Cookie和使用程序化API。详细解释了如何在门户服务器环境中处理首次登录请求时可能出现的LtpaToken缺失问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We have two options to do this


    * Cookie: You can use the LtpaToken from the cookie. This approach works in all cases but one, which is that as soon as you login the first request wont have this cookie.

    * Programmatic API: You can always read the LtpaToken from the cookie




 private String doesLTPATokenCookieExists(PortletRequest servletRequest){
  HttpServletRequest servletRequest =(PortletRequest)request;

  Cookie[] cookie =servletRequest.getCookies();
  for(int i = 0 ; i < cookie.length ;i++){
    System.out.println("Cookie Name " + cookie[i].getName() );
    if( cookie[i].getName().equals("LtpaToken"))
      return cookie[i].getValue();
    }
  return false;
}



In cases where the portlet is rendering on a first page that gets invoked after login the LtpaToken wont be there in the cookie. The basic idea is portal server will generate LtpaToken and send it in first response and thereafter the request will always have the LtpaToken cookie but that first request wont have the LtpaToken cookie, in those cases we can use this programmatic method for reading cookie


private  String getSecurityToken(){
  byte[] token = null;
  try{
    // Get current security subject
    Subject security_subject = WSSubject.getRunAsSubject();
    if (security_subject != null){
      // Get all security credentials from the security subject
      Set security_credentials =
          security_subject.getPublicCredentials(WSCredential.class);

      // Get the first credential
      WSCredential security_credential =
   (WSCredential)security_credentials.iterator().next();
      String user = (String) security_credential.getSecurityName();
      if(user.equalsIgnoreCase("UNAUTHENTICATED")){
        return null;
      }
      token = security_credential.getCredentialToken();
      if(token == null){
        return null;
      }
      String ltpaToken =
   com.ibm.ws.webservices.engine.encoding.Base64.encode(token);
      return ltpaToken;
    }
  }
  catch (Exception e){
    e.printStackTrace();
  }
  return null;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值