Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 9844285

Browse files
committed
Updated Meet Stormpath section of the tutorial.
1 parent bcb7792 commit 9844285

1 file changed

Lines changed: 161 additions & 29 deletions

File tree

docs/source/tutorial.rst

Lines changed: 161 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
Tutorial
44
========
55

6-
This tutorial will take you from zero to a Stormpath enabled application featuring Spring Boot WebMVC and Spring Security integration.
6+
This tutorial will take you from zero to a Stormpath enabled application featuring Spring #if($springboot)Boot#end WebMVC and Spring Security integration.
77

8-
It should take about 30 minutes from start to finish. If you are looking for a bare bones intro to using Stormpath and
9-
Spring Boot, check out the :doc:`quickstart`.
8+
It should take about 30 minutes from start to finish. If you are looking for a bare bones intro to using |project|, check out the :doc:`quickstart`.
9+
10+
#if( $springboot )
1011

1112
If you've already gone through the quickstart, jump over to the :ref:`spring-boot-meet-stormpath` section.
1213

@@ -15,6 +16,17 @@ Spring Boot, Spring Web MVC, Spring Security and the Thymeleaf templating engine
1516
such as Spring Security, can easily be disabled through the use of properties or via annotations. (You'll see an example of disabling
1617
Spring Security with properties in the :ref:`spring-boot-meet-stormpath` section).
1718

19+
#elseif( $spring )
20+
21+
If you've already gone through the quickstart, jump over to the :ref:`spring-meet-stormpath` section.
22+
23+
All of the code in the tutorial makes use of the ``stormpath-spring-security-webmvc`` integration. This integration has it all:
24+
Spring, Spring Web MVC, Spring Security and the JSP templating engine - all integrated with Stormpath. Component features,
25+
such as Spring Security, can easily be disabled through the use of properties or via annotations. (You'll see an example of disabling
26+
Spring Security with properties in the :ref:`spring-meet-stormpath` section).
27+
28+
#end
29+
1830
Topics:
1931

2032
.. contents::
@@ -23,27 +35,32 @@ Topics:
2335

2436
.. include:: stormpath-setup.txt
2537

26-
For the rest of the tutorial, we will be referring to the tutorial code found in `tutorials/spring-boot <https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot>`_.
38+
For the rest of the tutorial, we will be referring to the tutorial code found in #if($springboot)`tutorials/spring-boot`_ #elseif($spring)`tutorials/spring`_ #end.
39+
2740

2841
Each of the tutorial sections is completely standalone and can be used as a starting point for your own applications.
2942

30-
For instance, if you wanted to build a Spring Boot WebMVC project, including Spring Security integrated with Stormpath, you
43+
For instance, if you wanted to build a |project| project, including Spring Security integrated with Stormpath, you
3144
could do the following:
3245

46+
#if( $springboot )
47+
3348
.. code-block:: bash
3449
35-
mkdir MyProject
36-
cd MyProject
50+
cd <path to Stormpath sdk>
51+
checkout stormpath-sdk-root-${maven.project.artifactId}
52+
mkdir /MyProject
53+
cd /MyProject
3754
cp -r <path to Stormpath sdk>/tutorials/spring-boot/03-spring-security-refined/* .
3855
mvn clean package
39-
java -jar target/*.jar
56+
mvn spring-boot:run
4057
4158
.. _spring-boot-meet-stormpath:
4259

4360
Spring Boot: Meet Stormpath
4461
---------------------------
4562

46-
Let's fire up a basic Spring Boot Web application. The code for this section can be found in `tutorials/spring-boot/00-the-basics <https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/00-the-basics>`_.
63+
Let's fire up a basic Spring Boot WebMVC application. The code for this section can be found in `tutorials/spring-boot/00-the-basics`_.
4764

4865
Note: This assumes you have your ``apiKey.properties`` file in the standard location: ``~/.stormpath/apiKey.properties``.
4966

@@ -59,20 +76,19 @@ You should now be able to browse to `<http://localhost:${port}>`_ and see a welc
5976
This application has just two code files and a properties file in it. Here's the structure:
6077

6178
.. code-block:: bash
62-
:emphasize-lines: 9, 10
63-
64-
.
65-
`-- src
66-
`-- main
67-
|-- java
68-
| `-- com
69-
| `-- stormpath
70-
| `-- tutorial
71-
| |-- controller
72-
| | `-- HelloController.java
73-
| `-- Application.java
74-
`-- resources
75-
`-- application.properties
79+
:emphasize-lines: 7, 9
80+
81+
src
82+
└── main
83+
├── java
84+
│   └── com
85+
│   └── stormpath
86+
│   └── tutorial
87+
│   ├── Application.java
88+
│   └── controller
89+
│   └── HelloController.java
90+
└── resources
91+
└── application.properties
7692
7793
``Application.java`` is a most basic Spring Boot application file with a ``main`` method and the ``@SpringBootApplication``
7894
annotation:
@@ -88,23 +104,134 @@ annotation:
88104
}
89105
}
90106
91-
``HelloController.java`` is a little more interesting:
107+
#elseif( $spring )
108+
109+
.. code-block:: bash
110+
111+
cd <path to Stormpath sdk>
112+
checkout stormpath-sdk-root-${maven.project.artifactId}
113+
mkdir /MyProject
114+
cd /MyProject
115+
cp -r <path to Stormpath sdk>/tutorials/spring/03-spring-security-refined/* .
116+
mvn clean package
117+
mvn tomcat7:run
118+
119+
.. _spring-meet-stormpath:
120+
121+
Spring: Meet Stormpath
122+
----------------------
123+
124+
Let's fire up a basic Spring WebMVC application. The code for this section can be found in `tutorials/spring/00-the-basics`_.
125+
126+
Note: This assumes you have your ``apiKey.properties`` file in the standard location: ``~/.stormpath/apiKey.properties``.
127+
128+
To build and run, do this:
129+
130+
.. code-block:: bash
131+
132+
mvn clean package
133+
mvn tomcat7:run
134+
135+
You should now be able to browse to `<http://localhost:${port}>`_ and see a welcome message with your Stormpath application's name.
136+
137+
This application has just three code files, a properties file, and a web descriptor in it. Here's the structure:
138+
139+
.. code-block:: bash
140+
:emphasize-lines: 7, 9, 11
141+
142+
src
143+
└── main
144+
├── java
145+
│   └── com
146+
│   └── stormpath
147+
│   └── tutorial
148+
│   ├── WebAppInitializer.java
149+
│   ├── config
150+
│   │   └── WebAppConfig.java
151+
│   └── controller
152+
│   └── HelloController.java
153+
├── resources
154+
│   └── application.properties
155+
└── webapp
156+
└── WEB-INF
157+
└── web.xml
158+
159+
``WebAppInitializer.java`` is a basic Spring WebMVC application class. It overrides the ``onStartup`` method from the ``WebApplicationInitializer`` interface:
160+
161+
.. code-block:: java
162+
:linenos:
163+
:emphasize-lines: 1, 4, 7, 10, 15
164+
165+
public class WebAppInitializer implements WebApplicationInitializer {
166+
167+
@Override
168+
public void onStartup(ServletContext sc) throws ServletException {
169+
170+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
171+
context.register(WebAppConfig.class);
172+
sc.addListener(new ContextLoaderListener(context));
173+
174+
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
175+
ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", dispatcherServlet);
176+
dispatcher.setLoadOnStartup(1);
177+
dispatcher.addMapping("/");
178+
179+
FilterRegistration.Dynamic filter = sc.addFilter("stormpathFilter", new DelegatingFilterProxy());
180+
EnumSet<DispatcherType> types =
181+
EnumSet.of(DispatcherType.ERROR, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
182+
filter.addMappingForUrlPatterns(types, false, "/*");
183+
}
184+
}
185+
186+
There are three primary bits of setup going on here. Line 7 registers the ``WebAppConfig`` with the application (see below).
187+
Line 10 sets up the ``DispatcherServlet``. And, line 15 adds in the ``stormpathFilter``.
188+
189+
``WebAppConfig`` is where the Stormpath integration magic is happening:
92190

93191
.. code-block:: java
94192
:linenos:
95-
:emphasize-lines: 5,6
193+
:emphasize-lines: 1, 2, 5, 10
194+
195+
@EnableStormpath //Stormpath base beans
196+
@EnableStormpathWebMvc //Stormpath web mvc beans plus out-of-the-box views
197+
@EnableWebMvc
198+
@ComponentScan("com.stormpath.tutorial")
199+
@PropertySource("classpath:application.properties")
200+
@Configuration
201+
public class WebAppConfig {
202+
203+
@Bean
204+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
205+
return new PropertySourcesPlaceholderConfigurer();
206+
}
207+
}
208+
209+
Lines 1 and 2 enable Stormpath and Stormpath WebMVC. Lines 5 and 10 ensure that you can override that default
210+
``stormpath.*`` properties in your own ``application.properties`` file.
211+
212+
That's everything that's needed to get the views and the other features of Stormpath!
213+
214+
#end
215+
216+
``HelloController.java`` is a where we get our first taste of Stormpath in action:
217+
218+
.. code-block:: java
219+
:linenos:
220+
:emphasize-lines: 5, 9
96221
97222
@RestController
98223
public class HelloController {
224+
225+
@Autowired
226+
Application app;
227+
99228
@RequestMapping("/")
100-
public String hello(HttpServletRequest req) {
101-
Application app = ApplicationResolver.INSTANCE.getApplication(req);
229+
public String hello() {
102230
return "Hello, " + app.getName();
103231
}
104232
}
105233
106-
Here we have our first taste of Stormpath in action. On line 5 we are getting hold of the Stormpath application and on
107-
line 6 we are obtaining its name for display.
234+
On line 5 we are getting hold of the Stormpath Application and on line 9 we are obtaining its name for display.
108235

109236
For this example, we don't want Spring Security locking everything down, which is its default behavior. So, we will simply
110237
disable it. That's where the ``application.properties`` files comes in:
@@ -665,3 +792,8 @@ write a REST API that makes use of Spring Security that has no web layer.
665792

666793
Take a look at the `javadocs </java/apidocs>`_ as well as the `other code examples <https://github.com/stormpath/stormpath-sdk-java/tree/master/examples>`_
667794
for more information on all that the Stormpath Java SDK has to offer.
795+
796+
.. _tutorials/spring-boot: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot
797+
.. _tutorials/spring: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring
798+
.. _tutorials/spring-boot/00-the-basics: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/00-the-basics
799+
.. _tutorials/spring/00-the-basics: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/00-the-basics

0 commit comments

Comments
 (0)