|
@@ -2,14 +2,21 @@
|
|
|
:author: Rob Winch
|
|
|
:starter-appname: insecuremvc
|
|
|
:completed-appname: hellomvc-jc
|
|
|
-:include-dir: src/asciidoc/_hello-includes
|
|
|
-:verify-starter-app-include: verify-insecuremvc-app.asc
|
|
|
+:include-dir: _includes
|
|
|
+:hello-include-dir: _hello-includes
|
|
|
|
|
|
This guide provides instructions on how to add Spring Security to an existing Spring MVC application without the use of XML.
|
|
|
|
|
|
include::{include-dir}/setting-up-the-sample.asc[]
|
|
|
|
|
|
-include::{include-dir}/secure-the-application.asc[]
|
|
|
+Verify the application is working:
|
|
|
+
|
|
|
+* A page displaying a user's inbox can be seen at http://localhost:8080/sample/
|
|
|
+* Try clicking on the Compose link and creating a message. The message details should be displayed.
|
|
|
+* Now click on the Inbox link and see the message listed. You can click on the summary link to see the details displayed again.
|
|
|
+
|
|
|
+
|
|
|
+include::{hello-include-dir}/secure-the-application.asc[]
|
|
|
|
|
|
=== Registering Spring Security with the war
|
|
|
|
|
@@ -40,7 +47,7 @@ The `MessageSecurityWebApplicationInitializer` will automatically register the s
|
|
|
|
|
|
=== Verify SecurityConfig is loaded
|
|
|
|
|
|
-Just because <<security-config-java,`SecurityConfig`>> exists, does not mean that our Spring application knows about it. In this instance, our Spring root application context is initialized using MessageWebApplicationInitializer which is included with our spring-security-samples-messages-jc project. You can find a snippet of it below:
|
|
|
+Just because <<security-config-java,SecurityConfig>> exists, does not mean that our Spring application knows about it. In this instance, our Spring root application context is initialized using MessageWebApplicationInitializer which is included with our spring-security-samples-messages-jc project. You can find a snippet of it below:
|
|
|
|
|
|
[[message-web-application-inititializer-java]]
|
|
|
.MessageWebApplicationInitializer.java
|
|
@@ -66,18 +73,16 @@ You will notice it is loading the `RootConfiguration` class which is also includ
|
|
|
[source,java]
|
|
|
----
|
|
|
@Configuration
|
|
|
-@ComponentScan(value = "org.springframework.security.samples.config",
|
|
|
- excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = RootConfiguration.class))
|
|
|
+@ComponentScan
|
|
|
public class RootConfiguration {
|
|
|
-
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-The `@ComponentScan` is loading all configuration in the org.springframework.security.samples.config package. Since <<security-config-java,`SecurityConfig`>> is in this package, it will be loaded with our existing setup and there is nothing more to do.
|
|
|
+The `@ComponentScan` is loading all configuration within the same package (and child packags) as `RootConfiguration`. Since <<security-config-java,SecurityConfig>> is in this package, it will be loaded with our existing setup and there is nothing more to do.
|
|
|
|
|
|
-NOTE: Had <<security-config-java,`SecurityConfig`>> not been loaded, we could have used an `@Import(SecurityConfig)` above the class definition of <<root-configuration-java,`RootConfiguration`>> or added <<security-config-java,`SecurityConfig`>> as one of the results for `getRootConfigClasses()`.
|
|
|
+NOTE: Had <<security-config-java,SecurityConfig>> not been loaded, we could have used an `@Import(SecurityConfig)` above the class definition of <<root-configuration-java,RootConfiguration>> or added <<security-config-java,SecurityConfig>> as one of the results for `getRootConfigClasses()`.
|
|
|
|
|
|
-include::{include-dir}/exploring-the-secured-application.asc[]
|
|
|
+include::{hello-include-dir}/exploring-the-secured-application.asc[]
|
|
|
|
|
|
==== Displaying the user name
|
|
|
|
|
@@ -104,7 +109,7 @@ Now that we have authenticated, let's update the application to display the user
|
|
|
|
|
|
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.
|
|
|
|
|
|
-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>>
|
|
|
+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>>. Specifically, it is integrating with `HttpServletRequest#getRemoteUser()`.
|
|
|
|
|
|
==== Logging out
|
|
|
|
|
@@ -142,4 +147,4 @@ Refresh the page at http://localhost:8080/sample/ and you will see the log out b
|
|
|
|
|
|
== Conclusion
|
|
|
|
|
|
-You should now now how to secure your application using Spring Security without using any XML. Next, we will see how to link:form.html[customize our login form].
|
|
|
+You should now know how to secure your application using Spring Security without using any XML. Next, we will see how to link:form.html[customize our login form].
|