exploring-the-secured-application.asc 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. === Exploring the secured application
  2. Start the server as we did in <<running-the-{starter-appname}-application>> Now when you visit http://localhost:8080/sample/ you will be prompted with a login page that is automatically generated by Spring Security.
  3. ==== Authenticating to the secured application
  4. Try entering an invalid username and password:
  5. * *Username* _invalid_
  6. * *Password* _invalid_
  7. You should see an error message stating that authentication failed. Now try entering a valid username and password:
  8. * *Username* _user_
  9. * *Password* _password_
  10. You should now see the page that we wanted to secure.
  11. NOTE: The reason we can successfully authenticate with *Username* _user_ and *Password* _password_ is because that is what we configured in our <<security-config-java,`SecurityConfig`>>.
  12. ==== Displaying the user name
  13. Now that we have authenticated, let's update the application to display the username. Update the body of index.jsp to be the following:
  14. .src/main/webapp/index.jsp
  15. [source,html]
  16. ----
  17. <body>
  18. <div class="container">
  19. <h1>This is secured!</h1>
  20. <p>
  21. Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
  22. </p>
  23. </div>
  24. </body>
  25. ----
  26. 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.
  27. 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>>
  28. ==== Logging out
  29. 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 link as shown below:
  30. .src/main/webapp/index.jsp
  31. [source,html]
  32. ----
  33. <body>
  34. <div class="container">
  35. <h1>This is secured!</h1>
  36. <c:url var="logoutUrl" value="/logout"/>
  37. <p>
  38. Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
  39. </p>
  40. <p>
  41. <a href="${logoutUrl}">Click here</a> to log out.
  42. </p>
  43. </div>
  44. </body>
  45. ----
  46. Refresh the page at http://localhost:8080/sample/ and you will see the log out link. Click the link and see that the application logs you out successfully.
  47. ==== Basic authentication
  48. We stated that Spring Security supported both form and HTTP Basic authentication, but how does Spring Security know when to use one and not the other? When using HTTP Basic, the user should receive a HTTP 401 response, but when we visit our application in our web browser we are redirected to a login page. The reason for this is because Spring Security uses content negotiation to determine which type of authentication to use. For example, if we specified our *Accept* header to be _application/json_ the result would be an HTTP 401.
  49. You can use any tool you prefer (i.e. curl), but the instructions in this section we will use https://www.google.com/intl/en/chrome/browser/[Google Chrome] and the https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en[Postman - REST Client] to make an _application/json_ request to our application.
  50. * Open Google Chrome and launch the Postman - REST Client extension
  51. * Enter _http://localhost:8080/sample/_ into the request URL field
  52. * Select the *Headers* button
  53. * Enter _Accept_ into the *Header* input
  54. * Enter _application/json_ into the *Value* field
  55. * Presss the *Send* button
  56. Observe that we get an HTTP Status of 401 instead of our redirect. Now lets try entering our user name and password.
  57. * Select the *Basic Auth* tab
  58. * Enter _user_ for the *Username*
  59. * Enter _password_ for the *Password*
  60. * Click the *Refresh headers* button
  61. * Click the *Send* button
  62. This time you should see the HTML of our secured page.