Jump to content
We've recently updated our Privacy Statement, available here ×

Identifying the currently logged in user.


spierepf
Go to solution Solved by gdmoreno,

Recommended Posts

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution
Hi,

 

Try what you see below, you'll have to put into the java class that makes sense in your case.
Code:
// Add these imports to your Java class

import com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

// Put this in the method you need the username

String username; // will store logged-in username here

// Using Spring to pull authentication info
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getPrincipal() != null) {
username = ((MetadataUserDetails) auth.getPrincipal()).getUsername();
}

// Do what you need to do here



Link to comment
Share on other sites

Fortunately, it turns out that com.jaspersoft.jasperserver.api.metadata.user.domain.User is an interface, and com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails implements that interface.

So all you have to do is import that User interface and change the code only a little, like what's below.

Code:
// Import this interface, keep the other importsimport com.jaspersoft.jasperserver.api.metadata.user.domain.User;// Find this line: // username = ((MetadataUserDetails) auth.getPrincipal()).getUsername();// and put this in its placeusername = ((User) auth.getPrincipal()).getUsername();
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

I was trying to invoke the username in the follwoing way:

 

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

String username = principal.toString();

 

Now I can see username contains MetadataUserDetails, how can I grab the currently logged in user name?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...