secure-the-application.asc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 our application.
  3. === Updating your dependencies
  4. include::../{include-maven-repository}[]
  5. In order to use Spring Security you must add the necessary dependencies. For the sample we will add the following Spring Security dependencies:
  6. .pom.xml
  7. [source,xml]
  8. [subs="verbatim,attributes"]
  9. ----
  10. <dependencies>
  11. <!-- ... other dependency elements ... -->
  12. <dependency>
  13. <groupId>org.springframework.security</groupId>
  14. <artifactId>spring-security-web</artifactId>
  15. <version>{spring-security-version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.security</groupId>
  19. <artifactId>spring-security-config</artifactId>
  20. <version>{spring-security-version}</version>
  21. </dependency>
  22. </dependencies>
  23. ----
  24. After you have completed this, you need to ensure that STS knows about the updated dependencies by:
  25. * Right click on the _spring-security-samples-{starter-appname}_ application
  26. * Select *Maven->Update project...*
  27. * Ensure the project is selected, and click *OK*
  28. === Creating your Spring Security configuration
  29. The next step is to create a Spring Security configuration.
  30. * Right click the _spring-security-samples-{starter-appname}_ project the Package Explorer view
  31. * Select *New->Class*
  32. * Enter _org.springframework.security.samples.config_ for the *Package*
  33. * Enter _SecurityConfig_ for the *Name*
  34. * Click *Finish*
  35. * Replace the file with the following contents:
  36. [[security-config-java]]
  37. .src/main/java/org/springframework/security/samples/config/SecurityConfig.java
  38. [source,java]
  39. ----
  40. package org.springframework.security.samples.config;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.context.annotation.Configuration;
  43. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  44. import org.springframework.security.config.annotation.web.configuration.*;
  45. @EnableWebSecurity
  46. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  47. @Autowired
  48. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  49. auth
  50. .inMemoryAuthentication()
  51. .withUser("user").password("password").roles("USER");
  52. }
  53. }
  54. ----
  55. NOTE: The name of the configureGlobal method is not important. However, it is important to only configure AuthenticationManagerBuilder in a class annotated with either `@EnableWebSecurity`, `@EnableGlobalMethodSecurity`, or `@EnableGlobalAuthentication`. Doing otherwise has unpredictable results.
  56. [[servlet-api-integration]]
  57. The <<security-config-java,SecurityConfig>> will:
  58. * Require authentication to every URL in your application
  59. * Generate a login form for you
  60. * Allow the user with the *Username* _user_ and the *Password* _password_ to authenticate with form based authentication
  61. * Allow the user to logout
  62. * http://en.wikipedia.org/wiki/Cross-site_request_forgery[CSRF attack] prevention
  63. * http://en.wikipedia.org/wiki/Session_fixation[Session Fixation] protection
  64. * Security Header integration
  65. ** http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security[HTTP Strict Transport Security] for secure requests
  66. ** http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx[X-Content-Type-Options] integration
  67. ** Cache Control (can be overridden later by your application to allow caching of your static resources)
  68. ** http://msdn.microsoft.com/en-us/library/dd565647(v=vs.85).aspx[X-XSS-Protection] integration
  69. ** X-Frame-Options integration to help prevent http://en.wikipedia.org/wiki/Clickjacking[Clickjacking]
  70. * Integrate with the following Servlet API methods
  71. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRemoteUser()[HttpServletRequest#getRemoteUser()]
  72. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getUserPrincipal()[HttpServletRequest.html#getUserPrincipal()]
  73. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#isUserInRole(java.lang.String)[HttpServletRequest.html#isUserInRole(java.lang.String)]
  74. ** 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)]
  75. ** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#logout()[HttpServletRequest.html#logout()]