cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Current User name in java backed webscript

yasosaran
Champ in-the-making
Champ in-the-making
Hello Everyone,

How to get current user name in share. I have a custom java backed dashlet, and I would like get the current user name in the custom dashlet and do some query based on that and put those results in the model.

I am not sure how to get the current user name. I tried using AuthenticationUtil.getRunAsUser() and it is returning null. I also tried authenticationService .getCurrentUsername() and add the reference in my bean descriptor,, and its throwing No such bean defined exception.

Could someone please help me?

Thanks
5 REPLIES 5

jayjayecl
Confirmed Champ
Confirmed Champ
the "authenticationService.getCurrentUserName();" is the right choice (I use it myself)

Maybe you could give us your bean descriptor (web-scripts-application-context.xml) and your Java class, so that we find the problem ?

sanket
Champ on-the-rise
Champ on-the-rise
Hi Guise.
I am using the same thing.
getAuthenticationService().getCurrentUserName()

But it always returns "admin".

It should return the name of the logged in user, but it's not happening.

Please help.

Thanks in advance.

waynejiang
Champ in-the-making
Champ in-the-making
If you need get the current user in java backed webscript frm Share. Code as below,
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.extensions.webscripts.connector.User;

public class DemoAction extends AbstractWebScript {
    @Override
    public void execute(WebScriptRequest request, WebScriptResponse response) {
        RequestContext context = ThreadLocalRequestContext.getRequestContext();
        User user = context.getUser();
    }
}

Hello Waynejiang,

The above mentioned gives a NullPointerException. Could you pls tell if the User has to be set somewhere else.kindly help

Thanks

kevinchen
Champ in-the-making
Champ in-the-making

String user = AuthenticationUtil.getFullyAuthenticatedUser();