secure-the-application.asc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. == Securing the application
  2. Before securing your application, it is important to ensure that the existing application works as we did in <<running-the-{starter-appname}-application>>. Now that the application runs without security, we are ready to add security to our application. This section demonstrates the minimal steps to add Spring Security to a Spring MVC application.
  3. === Updating your dependencies
  4. You will need to ensure you have added the dependencies. Spring Security milestones and release candidates are available in the https://github.com/SpringSource/spring-framework/wiki/SpringSource-repository-FAQ[Spring Milestone Repository]. In short, if you are using Maven and using a milestone or release candidates ensure you have the following repository in your pom.xml:
  5. .pom.xml
  6. [source,xml]
  7. ----
  8. <repositories>
  9. <!-- ... possibly other repository elements ... -->
  10. <repository>
  11. <id>spring-libs-milestone</id>
  12. <name>Spring Milestone Repository</name>
  13. <url>http://repo.springsource.org/milestone</url>
  14. </repository>
  15. </repositories>
  16. ----
  17. You will then need to include the Spring Security dependencies
  18. .pom.xml
  19. [source,xml]
  20. [subs="verbatim,attributes"]
  21. ----
  22. <dependencies>
  23. <!-- ... other dependency elements ... -->
  24. <dependency>
  25. <groupId>org.springframework.security</groupId>
  26. <artifactId>spring-security-web</artifactId>
  27. <version>{spring-security-version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.security</groupId>
  31. <artifactId>spring-security-config</artifactId>
  32. <version>{spring-security-version}</version>
  33. </dependency>
  34. </dependencies>
  35. ----
  36. After you have completed this, you need to ensure that STS knows about the updated dependencies by:
  37. * Right click on the _spring-security-samples-{starter-appname}_ application
  38. * Select *Maven->Update project...*
  39. * Ensure the project is selected, and click *OK*
  40. === Creating your Spring Security configuration
  41. The next step is to create a Spring Security configuration.
  42. * Right click the _spring-security-samples-{starter-appname}_ project the Package Explorer view
  43. * Select *New->Class*
  44. * Enter _org.springframework.security.samples.config_ for the *Package*
  45. * Enter _SecurityConfig_ for the *Name*
  46. * Click *Finish*
  47. * Replace the file with the following contents:
  48. [[security-config-java]]
  49. .src/main/java/org/springframework/security/samples/config/SecurityConfig.java
  50. [source,java]
  51. ----
  52. package org.springframework.security.samples.config;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.context.annotation.*;
  55. import org.springframework.security.config.annotation.authentication.builders.*;
  56. import org.springframework.security.config.annotation.web.configuration.*;
  57. @Configuration
  58. @EnableWebSecurity
  59. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  60. @Autowired
  61. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  62. auth
  63. .inMemoryAuthentication()
  64. .withUser("user").password("password").roles("USER");
  65. }
  66. }
  67. ----
  68. [[servlet-api-integration]]
  69. The <<security-config-java,`SecurityConfig`>> will:
  70. * Require authentication to every URL in your application
  71. * Generate a login form for you
  72. * Allow the user with the *Username* _user_ and the *Password* _password_ to authenticate with form based authentication
  73. * Allow the user to logout
  74. * http://en.wikipedia.org/wiki/Cross-site_request_forgery[CSRF attack] prevention
  75. * http://en.wikipedia.org/wiki/Session_fixation[Session Fixation] protection
  76. * Security Header integration
  77. ** http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security[HTTP Strict Transport Security] for secure requests
  78. ** http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx[X-Content-Type-Options] integration
  79. ** Cache Control (can be overridden later by your application to allow caching of your static resources)
  80. ** http://msdn.microsoft.com/en-us/library/dd565647(v=vs.85).aspx[X-XSS-Protection] integration
  81. ** X-Frame-Options integration to help prevent http://en.wikipedia.org/wiki/Clickjacking[Clickjacking]
  82. * Integrate with the following Servlet API methods
  83. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRemoteUser()[HttpServletRequest#getRemoteUser()]
  84. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getUserPrincipal()[HttpServletRequest.html#getUserPrincipal()]
  85. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#isUserInRole(java.lang.String)[HttpServletRequest.html#isUserInRole(java.lang.String)]
  86. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#login(java.lang.String,%20java.lang.String)[HttpServletRequest.html#login(java.lang.String, java.lang.String)]
  87. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#logout()[HttpServletRequest.html#logout()]