cancel
Showing results for 
Search instead for 
Did you mean: 

Howto log user if i know userID and passwd

Former Member
0 Kudos

Hi,

i need to make some iview which can be viewed with an anonymouse user and this user has to have possibility to authenticate himself from this iview. Is there any possibility to log user from java code without redirecting to login page(forceLogginUser)-in case if it is not possible what is the best way to store data which user filled in before?

thanks JJ

PS: I found out that i should propably use method logon from interface ILogonAuthentication but i dont know how can i send that userId and passwd. Shoud i use uidpwdlogon

authentication scheme and put in a request that parameters?

Accepted Solutions (1)

Accepted Solutions (1)

detlev_beutner
Active Contributor
0 Kudos

Hi JJ,

see JavaDoc for the logon method, https://media.sdn.sap.com/javadocs/NW04/SP9/ume/com/sap/security/api/logon/ILogonAuthentication.html

"The standard mechanism does the following: Looks up for the parameter j_user and j_password (pls. see the constants ILoginConstants.LOGON_UID_ALIAS and ILoginConstants.LOGON_PWD_ALIAS) in the servlet request and uses them for logon against the user repository that's configured."

So add uid and password to the request object.

For the authscheme parameter, there is no explicit documentation, I would decompile the logon component, then you will find it quite fast, I think.

Hope it helps

Detlev

Former Member
0 Kudos

Thanks,

i tried it and it propably works, but than i have problem with getting IUser object from set of principals. In javadoc is written: "In order to get an IUser object from this subject, call Subject.getPrincipals() and iterate through the returned Set of principals." so i tried to iterate but i was not able to get that IUser with any casting. Do u know how it should be?

Set principals = logonAuth.logon(request, response, "uidpwdlogon").getPrincipals();

I also tried st like this

loggedUser = UMFactory.getAuthenticator().getLoggedInUser(request, response);

but loggedUser is not authenticated.

thnaks

JJ

detlev_beutner
Active Contributor
0 Kudos

Hi JJ,

> tried to iterate but i was not able

> to get that IUser with any casting

?! Wasn't a problem, the following code works:

package com.btexx.test;
 
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sap.security.api.IUser;
import com.sap.security.api.UMFactory;
import com.sap.security.api.logon.ILoginConstants;
import com.sapportals.portal.prt.component.AbstractPortalComponent;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentResponse;

public class MyExample extends AbstractPortalComponent {
  public void doContent(IPortalComponentRequest request, IPortalComponentResponse response) {
    HttpServletRequest req = request.getServletRequest();
    HttpServletResponse res = request.getServletResponse(true);
    req.setAttribute(ILoginConstants.LOGON_UID_ALIAS, "testtest8");
    req.setAttribute(ILoginConstants.LOGON_PWD_ALIAS, "testtest");
    try {
      Set principals = UMFactory.getLogonAuthenticator().logon(req, res, "uidpwdlogon").getPrincipals();
      Iterator it = principals.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        if (o instanceof IUser) {
          IUser myUser = (IUser) o;                
          res.getWriter().write("Got it!<br>");
          res.getWriter().write("His mail for example: " + myUser.getEmail());
        }
      }
    } catch (LoginException e) {
      // TODO check against logon failures (USER_AUTH_FAILED etc)
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Hope it helps

Detlev

Answers (0)