HttpServletRequest.getRemoteUser() vs HttpServletRequest.getUserPrincipal().getName()

前端 未结 3 1183
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 16:44

These two seem to be doing the same things. Can anyone explain the main difference between the two? When would you use one vs the other?

HttpServletRequest.getRemoteU

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 17:32

    A bit related issue:

    People converting older IBM Portlet API code to JSR168 one had to change PortletRequest to HttpServletRequest in some method parameters, but then from WPS6.1 and up they can't cast that to PortletRequest (it doesn't implement the respective interface anymore as it seems) and if they call "getRemoteUser" directly on the HttpServletRequest they get back null (some say a workarround is to enable application security option in WAS [WebSphere Application Server], others say more security-related markup is needed in web.xml)

    A workarround seems to be to use PUMA, but of course that is IBM WebSphere specific. Probably at other Portlet Containers there are other vendor-specific workarrounds if one finds that getRemoteUser always returns null (judging from other replies then getUserPrincipal().getName() also returns null if getRemoteUser is implemented as just a shortcut to that one).

    BTW, the PUMA code I mention above is here, since it's a bit hard to find what works in WPS6.1+:

    import com.ibm.portal.portlet.service.PortletServiceHome;
    import com.ibm.portal.um.*;
    import com.ibm.portal.um.exceptions.PumaException;
    import com.ibm.portal.puma.User;
    
    //...
    
    public String getCurrentUser(){
      try {
        Context ctx = new InitialContext();
        Name myjndiname = new CompositeName(PumaHome.JNDI_NAME);
        PumaHome myHome = (PumaHome) ctx.lookup(myjndiname); 
        if (myHome!=null) {
          PumaProfile pumaProfile = myHome.getProfile();
          com.ibm.portal.um.User user = (com.ibm.portal.um.User)pumaProfile.getCurrentUser();
          List attributes = new ArrayList();
          attributes.add("uid");
          Map userAttributes = pumaProfile.getAttributes(user,attributes);
          return (String) userAttributes.get("uid");
        }
      }
    

提交回复
热议问题