PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/adding-websudo-support-to-your-plugin.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 130 lines | 101 code | 29 blank | 0 comment | 0 complexity | f82617b452083859866eb9972ea784cb MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/adding-websudo-support-to-your-plugin-5242916.html
  4. - /server/framework/atlassian-sdk/adding-websudo-support-to-your-plugin-5242916.md
  5. category: devguide
  6. confluence_id: 5242916
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=5242916
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=5242916
  9. date: '2017-12-08'
  10. guides: guides
  11. legacy_title: Adding WebSudo Support to your Plugin
  12. platform: server
  13. product: atlassian-sdk
  14. subcategory: learning
  15. title: Adding WebSudo support to your plugin
  16. ---
  17. # Adding WebSudo support to your plugin
  18. <table>
  19. <colgroup>
  20. <col style="width: 50%" />
  21. <col style="width: 50%" />
  22. </colgroup>
  23. <tbody>
  24. <tr class="odd">
  25. <td><p>Available:</p></td>
  26. <td><p><a href="https://developer.atlassian.com/pages/viewpage.action?pageId=5242917">SAL 2.2</a> and later; <a href="/server/framework/atlassian-sdk/rest-plugin-2-2-release-notes">REST 2.2</a> and later.</p></td>
  27. </tr>
  28. </tbody>
  29. </table>
  30. Support for Secure Administrator Sessions (also called websudo) was added in Confluence 3.3 and JIRA 4.3. When an administrator who is logged into Confluence or JIRA attempts to access an administration function, they are prompted to log in again.  By default, Atlassian applications run with secure sessions enabled. Administrators can disable this feature. For information on how to do this, refer to the <a href="http://confluence.atlassian.com" class="external-link">administrative documentation</a> for your product and version.  
  31. All the Atlassian applications will support WebSudo sessions at some point. As of SAL version 2.2 and REST 2.2 it is possible to enforce websudo from within a plugin if the host application supports it.
  32. SAL 2.2 supports programmatic access to a `WebSudoManager` that you can use from within your [servlet](/server/framework/atlassian-sdk/servlet-plugin-module) or [servlet filter](/server/framework/atlassian-sdk/servlet-filter-plugin-module). As of version 2.2 of the Atlassian [REST plugin module](https://developer.atlassian.com/display/REST), you can add annotations to REST resources.
  33. ## Servlet Example
  34. You can use the `com.atlassian.sal.api.websudo.WebSudoManager` to check for secure administrator sessions and to enforce the websudo protection for the current request.
  35. The call to `WebSudoManager#enforceWebSudoProtection(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)` will cause the host application to
  36. redirect the user to an authentication form if, and only if, the current request is not WebSudo protected.
  37. ``` java
  38. package com.example.myplugin.servlet;
  39. import static com.google.common.base.Preconditions.checkNotNull;
  40. // import [...]
  41. public final class MyManagerServlet extends HttpServlet
  42. {
  43. private final UserManager userManager;
  44. private final WebSudoManager webSudoManager;
  45. public MyManagerServlet(final UserManager userManager,
  46. final WebSudoManager webSudoManager)
  47. {
  48. this.userManager = checkNotNull(userManager, "userManager");
  49. this.webSudoManager = checkNotNull(webSudoManager, "webSudoManager");
  50. }
  51. @Override
  52. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  53. {
  54. // WebSudo
  55. try {
  56. webSudoManager.willExecuteWebSudoRequest(request);
  57. // This request will be WebSudo protected
  58. // Add your custom code here
  59. } catch(WebSudoSessionException wes) {
  60. webSudoManager.enforceWebSudoProtection(request, response);
  61. }
  62. }
  63. @Override
  64. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  65. {
  66. // WebSudo
  67. try {
  68. webSudoManager.willExecuteWebSudoRequest(request);
  69. // This request will be WebSudo protected // Add your custom code here
  70. } catch(WebSudoSessionException wes) { // Send an error or redirect the user to the initial form.
  71. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  72. } }
  73. }
  74. ```
  75. ## REST Example
  76. SAL provides two annotations that you can use to control secure administrator sessions. You can apply the annotations on a package, type or method level.
  77. The `com.atlassian.sal.api.websudo.WebSudoRequired` annotation will require websudo protection. On the other hand, `com.atlassian.sal.api.websudo.WebSudoNotRequired` allows REST resources to bypass websudo protection if this annotation is applied to a more specific element.
  78. The following example adds a package level annotation that enforces websudo protection but allows the REST resource `ATestResource` to bypass it.
  79. Enforce websudo protection for all the resources in the `com.example.myplugin.rest.resources.admin` package:
  80. `com/example/myplugin/rest/resources/admin/package-info.java`:
  81. ``` java
  82. @WebSudoRequired
  83. package com.example.myplugin.rest.resources.admin;
  84. import com.atlassian.sal.api.websudo.WebSudoRequired;
  85. ```
  86. To exclude a resource, you can add the annotation `com.atlassian.sal.api.websudo.WebSudoNotRequired`:
  87. ``` java
  88. @Path("/test/{key}")
  89. @WebSudoNotRequired
  90. public class ATestResource
  91. {
  92. // [...]
  93. @GET
  94. public Response get(@PathParam("key") String key)
  95. {
  96. // ....
  97. return ...
  98. }
  99. }
  100. ```
  101. This prevents websudo protection from being enforced for the `ATestResource` REST resource.