helloworld.asc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. = Hello Spring Security Java Config
  2. :author: Rob Winch
  3. :starter-appname: insecure
  4. :completed-appname: helloworld-jc
  5. :include-dir: _includes
  6. :hello-include-dir: _hello-includes
  7. This guide provides instructions on how to add Spring Security to an existing application without the use of XML.
  8. include::{include-dir}/setting-up-the-sample.asc[]
  9. Verify the application is working by ensuring a page stating *TODO Secure this* is displayed at http://localhost:8080/sample/
  10. Once you have verified the application runs, stop the application server using the following steps:
  11. * In the Servers view select the latest tc Server
  12. * Click the stop button (a red square) to stop the application server
  13. include::{hello-include-dir}/secure-the-application.asc[]
  14. === Registering Spring Security with the war
  15. We have created the Spring Security configuration, but we still need to register it with the war. This can be done using the following steps:
  16. * Navigate to the *Package Explorer* view
  17. * Right click the *org.springframework.security.samples.config* package within the *spring-security-samples-{starter-appname}* project
  18. * Select *New->Class*
  19. * Enter _SecurityWebApplicationInitializer_ for the *Name*
  20. * Click *Finish*
  21. * Replace the file with the following contents:
  22. .src/main/java/org/springframework/security/samples/config/SecurityWebApplicationInitializer.java
  23. [source,java]
  24. ----
  25. package org.springframework.security.samples.config;
  26. import org.springframework.security.web.context.*;
  27. public class SecurityWebApplicationInitializer
  28. extends AbstractSecurityWebApplicationInitializer {
  29. public SecurityWebApplicationInitializer() {
  30. super(SecurityConfig.class);
  31. }
  32. }
  33. ----
  34. The `SecurityWebApplicationInitializer` will do the following things:
  35. * Automatically register the springSecurityFilterChain Filter for every URL in your application
  36. * Add a ContextLoaderListener that loads the <<security-config-java,SecurityConfig>>.
  37. NOTE: Since we were not already using Spring, this is a simple way to add our <<security-config-java,SecurityConfig>>. If we were already using Spring, then we should add our <<security-config-java,SecurityConfig>> with the reset of our Spring configuration (i.e. a subclass of AbstractContextLoaderInitializer or AbstractDispatcherServletInitializer) and use the default constructor instead.
  38. include::{hello-include-dir}/exploring-the-secured-application.asc[]
  39. ==== Displaying the user name
  40. Now that we have authenticated, let's update the application to display the username. Update the body of index.jsp to be the following:
  41. .src/main/webapp/index.jsp
  42. [source,html]
  43. ----
  44. <body>
  45. <div class="container">
  46. <h1>This is secured!</h1>
  47. <p>
  48. Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
  49. </p>
  50. </div>
  51. </body>
  52. ----
  53. WARNING: The `<c:out />` tag ensures the username is escaped to avoid http://en.wikipedia.org/wiki/Cross-site_scripting[XSS vulnerabilities] Regardless of how an application renders user inputed values, it should ensure that the values are properly escaped.
  54. Refresh the page at http://localhost:8080/sample/ and you will see the user name displayed. This works because Spring Security integrates with the <<servlet-api-integration,Servlet API methods>>
  55. ==== Logging out
  56. Now that we can view the user name, let's update the application to allow logging out. Update the body of index.jsp to contain a log out form as shown below:
  57. .src/main/webapp/index.jsp
  58. [source,html]
  59. ----
  60. <body>
  61. <div class="container">
  62. <h1>This is secured!</h1>
  63. <p>
  64. Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
  65. </p>
  66. <c:url var="logoutUrl" value="/logout"/>
  67. <form class="form-inline" action="${logoutUrl}" method="post">
  68. <input type="submit" value="Log out" />
  69. <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
  70. </form>
  71. </div>
  72. </body>
  73. ----
  74. In order to help protect against http://en.wikipedia.org/wiki/Cross-site_request_forgery[CSRF attacks], by default, Spring Security Java Configuration log out requires:
  75. * the HTTP method must be a POST
  76. * the CSRF token must be added to the request You can access it on the ServletRequest using the attribute _csrf as illustrated above.
  77. NOTE: If you were using Spring MVC's tag library or Thymeleaf, the CSRF token is automatically added as a hidden input for you.
  78. Refresh the page at http://localhost:8080/sample/ and you will see the log out button. Click the logout button and see that the application logs you out successfully.
  79. == Conclusion
  80. You should now know how to secure your application using Spring Security without using any XML. To learn more refer to the link:index.html[Spring Security Guides index page].