|
| 1 | +[[servlet-hello-xml]] |
| 2 | += Hello Spring Security (XML) |
| 3 | + |
| 4 | +This section covers how to use Spring Security with XML Configuration. |
| 5 | +For how to use Spring Security with Java configuration, see <<servlet-hello-jc>>. |
| 6 | +For how to use Spring Security with Spring Boot configuration, see <<servlet-hello-boot>>. |
| 7 | + |
| 8 | +== Updating Dependencies |
| 9 | + |
| 10 | +The first step is to update the dependencies by using <<maven-without-spring-boot,Maven>> or <<gradle-without-spring-boot,Gradle>>. |
| 11 | + |
| 12 | + |
| 13 | +[[servlet-hello-xml-http]] |
| 14 | +== Minimal `<http>` Configuration |
| 15 | + |
| 16 | +In this section, we discuss how to use Spring Security with XML Configuration. |
| 17 | + |
| 18 | +NOTE: The completed application can be found at {gh-samples-url}/xml/helloworld[samples/xml/helloworld] |
| 19 | +// FIXME: Link to Java Configuration and Boot |
| 20 | + |
| 21 | +The first step is to create our Spring Security XML Configuration. |
| 22 | +The configuration creates a Servlet `Filter` (known as the `springSecurityFilterChain`), which is responsible for all the security (protecting the application URLs, validating submitted username and passwords, redirecting to the log in form, and so on) within your application. |
| 23 | +The following example shows the most basic example of a Spring Security XML Configuration: |
| 24 | + |
| 25 | +.src/main/webapp/WEB-INF/spring/security.xml |
| 26 | +==== |
| 27 | +[source,xml] |
| 28 | +---- |
| 29 | +<b:beans xmlns="http://www.springframework.org/schema/security" |
| 30 | + xmlns:b="http://www.springframework.org/schema/beans" |
| 31 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 32 | + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| 33 | + http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> |
| 34 | + <http /> |
| 35 | +
|
| 36 | + <user-service> |
| 37 | + <user name="user" password="{noop}password" authorities="ROLE_USER" /> |
| 38 | + </user-service> |
| 39 | +</b:beans> |
| 40 | +
|
| 41 | +---- |
| 42 | +==== |
| 43 | + |
| 44 | + |
| 45 | +There really is not much to this configuration, but it does a lot. |
| 46 | +A summary of the features follows: |
| 47 | + |
| 48 | +* Require an authenticated user for any interaction with the application |
| 49 | +* Generate a default login form for you |
| 50 | +* Lets the user with a username of `user` and a password of `password` authenticate with form-based authentication |
| 51 | +* Protects the password storage with BCrypt |
| 52 | +* Lets the user to log out |
| 53 | +* http://en.wikipedia.org/wiki/Cross-site_request_forgery[CSRF attack] prevention |
| 54 | +* http://en.wikipedia.org/wiki/Session_fixation[Session Fixation] protection |
| 55 | +* Security Header integration |
| 56 | +** http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security[HTTP Strict Transport Security] for secure requests |
| 57 | +** http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx[X-Content-Type-Options] integration |
| 58 | +** Cache Control (can be overridden later by your application to allow caching of your static resources) |
| 59 | +** http://msdn.microsoft.com/en-us/library/dd565647(v=vs.85).aspx[X-XSS-Protection] integration |
| 60 | +** X-Frame-Options integration to help prevent http://en.wikipedia.org/wiki/Clickjacking[Clickjacking] |
| 61 | +* Integrate with the following Servlet API methods: |
| 62 | +** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRemoteUser()[`HttpServletRequest#getRemoteUser()`] |
| 63 | +** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getUserPrincipal()[`HttpServletRequest.html#getUserPrincipal()`] |
| 64 | +** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#isUserInRole(java.lang.String)[`HttpServletRequest.html#isUserInRole(java.lang.String)`] |
| 65 | +** 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)`] |
| 66 | +** http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#logout()[`HttpServletRequest.html#logout()`] |
| 67 | + |
| 68 | +// FIXME: After completed rewriting, link to all the sections of doc that this relates to |
| 69 | + |
| 70 | + |
| 71 | +[[servlet-hello-xml-webxml]] |
| 72 | +== `web.xml` Configuration |
| 73 | + |
| 74 | +The next step is to ensure that our Security configuration is being read in. |
| 75 | +To do so, we need to ensure a `ContextLoaderListener` is registered and the `contextConfigLocation` is including the configuration. |
| 76 | +The following example shows how to do so: |
| 77 | + |
| 78 | +.src/main/webapp/WEB-INF/web.xml |
| 79 | +==== |
| 80 | +[source,xml] |
| 81 | +---- |
| 82 | +<?xml version="1.0" encoding="UTF-8"?> |
| 83 | +<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" |
| 84 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 85 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee |
| 86 | + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> |
| 87 | +
|
| 88 | + <!-- |
| 89 | + Loads the Spring configurations from contextConfigLocation |
| 90 | + --> |
| 91 | + <listener> |
| 92 | + <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| 93 | + </listener> |
| 94 | +
|
| 95 | + <!-- |
| 96 | + The locations of the Spring Configuration. In this case, all configuration is |
| 97 | + in /WEB-INF/spring/ |
| 98 | + --> |
| 99 | + <context-param> |
| 100 | + <param-name>contextConfigLocation</param-name> |
| 101 | + <param-value> |
| 102 | + /WEB-INF/spring/*.xml |
| 103 | + </param-value> |
| 104 | + </context-param> |
| 105 | +
|
| 106 | + <!-- |
| 107 | + DelegatingFilterProxy looks for a Spring bean by the name of filter (springSecurityFilterChain) and delegates |
| 108 | + all work to that Bean. This is how the Servlet Container can a Spring Bean to act as a Servlet Filter. |
| 109 | + --> |
| 110 | + <filter> |
| 111 | + <filter-name>springSecurityFilterChain</filter-name> |
| 112 | + <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> |
| 113 | + </filter> |
| 114 | + <filter-mapping> |
| 115 | + <filter-name>springSecurityFilterChain</filter-name> |
| 116 | + <url-pattern>/*</url-pattern> |
| 117 | + </filter-mapping> |
| 118 | +
|
| 119 | +</web-app> |
| 120 | +---- |
| 121 | +==== |
| 122 | + |
| 123 | +[NOTE] |
| 124 | +==== |
| 125 | +If you integrate with an existing Spring MVC application, be sure to configure the `DispatcherServlet` to load the configuration from the root `ApplicationContext`. |
| 126 | +The following example shows how to do so: |
| 127 | +
|
| 128 | +===== |
| 129 | +.src/main/webapp/WEB-INF/web.xml |
| 130 | +[source,xml] |
| 131 | +---- |
| 132 | +<servlet> |
| 133 | + <servlet-name>spring</servlet-name> |
| 134 | + <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| 135 | + <!-- Load Spring MVC configuration from root ApplicationContext (context-param from above) --> |
| 136 | + <init-param> |
| 137 | + <param-name>contextConfigLocation</param-name> |
| 138 | + <param-value></param-value> |
| 139 | + </init-param> |
| 140 | +</servlet> |
| 141 | +
|
| 142 | +<servlet-mapping> |
| 143 | + <servlet-name>spring</servlet-name> |
| 144 | + <url-pattern>/</url-pattern> |
| 145 | +</servlet-mapping> |
| 146 | +---- |
| 147 | +===== |
| 148 | +==== |
0 commit comments