-->

Get Credentials from Current User in Atlassian Plu

2019-07-24 21:35发布

问题:

is it possible to get Username and Password in a Confluence Plugin? The Usernam works for me like this:

ApplicationUser user = userUtil.getUserByName(userManager.getRemoteUsername(request));

I want to connect to the Jira Rest Api with the current User.

回答1:

As @Philipp Sander already commented, it is not possible to retrieve the user's password. Confluence only stores passwords in an encrypted way, so it doesn't even know user passwords.

However, since you're talking about a Confluence plugin, there is another way to talk to other Atlassian applications' REST APIs by using application links.

If you require users to configure an application link to JIRA in their Confluence, then your plugin can get that ApplicationLink instance using the ApplicationLinkService's getApplicationLinks method:

java.lang.Iterable<ApplicationLink> getApplicationLinks(java.lang.Class<? extends ApplicationType> type) 

    Retrieves all ApplicationLinks of a particular ApplicationType.

Next, you can call the createAuthenticatedRequestFactory method on your ApplicationLink instance, e.g.:

ApplicationLinkRequestFactory   createAuthenticatedRequestFactory() 

    The ApplicationLinkRequestFactory returned by this method will choose a single AuthenticationProvider for automatically authenticating created Request objects.

And the RequestFactory makes it possible to send REST requests to the application you're targetting, ie. JIRA.

For more info, you can also check the SAL API documentation which has an example about how to use a RequestFactory.

There's also this useful related question on Atlassian Community that explains how you can get a RequestFactory to marshall objects using JAXB.