spierepf Posted June 25, 2012 Share Posted June 25, 2012 I am writing a custom data source that needs to identify the currently logged in user in order to correctly determine which database to connect to.How can I determine the currently logged in user from Java?Thanks,Peter. Link to comment Share on other sites More sharing options...
Solution gdmoreno Posted June 25, 2012 Solution Share Posted June 25, 2012 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 More sharing options...
spierepf Posted June 26, 2012 Author Share Posted June 26, 2012 Thanks gdmoreno, I was actually hoping for an instance of the: com.jaspersoft.jasperserver.api.metadata.user.domain.User class. Any ideas? Peter. Link to comment Share on other sites More sharing options...
gdmoreno Posted June 28, 2012 Share Posted June 28, 2012 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 More sharing options...
spierepf Posted June 28, 2012 Author Share Posted June 28, 2012 Worked like a charm! Awesome, thanks so much. Peter. Link to comment Share on other sites More sharing options...
Ankur Gupta Posted March 11, 2014 Share Posted March 11, 2014 Is there a way that we could get the username of the user who logged into the JasperServer Thanks Link to comment Share on other sites More sharing options...
farnaws Posted August 5, 2015 Share Posted August 5, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now