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

Commit 681effd

Browse files
committed
Updated Stormpath: Meet Spring Security section of the tutorial.
1 parent 2009f59 commit 681effd

File tree

1 file changed

+81
-26
lines changed

1 file changed

+81
-26
lines changed

docs/source/tutorial.rst

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,29 +353,61 @@ methods.
353353

354354
The official Spring Security documentation is at `http://projects.spring.io/spring-security <http://projects.spring.io/spring-security/>`_.
355355

356-
Let's take a look at the additions and changes to the project. The code for this section can be found in
357-
`tutorials/spring-boot/02-spring-security-ftw <https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/02-spring-security-ftw>`_.
356+
Let's take a look at the additions and changes to the project.
357+
358+
#if( $springboot )
359+
The code for this section can be found in `tutorials/spring-boot/02-spring-security-ftw`_.
360+
#elseif( $spring )
361+
The code for this section can be found in `tutorials/spring/02-spring-security-ftw`_.
362+
#end
358363

359364
We've added a configuration file called ``SpringSecurityWebAppConfig.java``. How does Spring know it's a configuration file?
360365
It has the ``@Configuration`` annotation:
361366

367+
#if( $springboot )
362368
.. code-block:: java
363369
:linenos:
364-
:emphasize-lines: 8
365-
366-
@Configuration
367-
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
368-
@Override
369-
protected void doConfigure(HttpSecurity http) throws Exception {
370-
http
371-
.authorizeRequests()
372-
.antMatchers("/").permitAll();
373-
}
374-
}
375-
376-
In order to easily hook into the Stormpath Spring Security integration, simply add a ``WebSecurityConfigurerAdapter`` in the application.
377-
Doing that just sets up all of the default views and hooks the Stormpath ``AuthenticationManager``
378-
into your application.
370+
371+
@Configuration
372+
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
373+
@Override
374+
protected void configure(HttpSecurity http) throws Exception {
375+
http
376+
.authorizeRequests()
377+
.antMatchers("/").permitAll();
378+
}
379+
}
380+
#elseif( $spring )
381+
.. code-block:: java
382+
:linenos:
383+
:emphasize-lines: 12
384+
385+
import static com.stormpath.spring.config.StormpathWebSecurityConfigurer.stormpath;
386+
387+
@Configuration
388+
@ComponentScan("com.stormpath.tutorial")
389+
@PropertySource("classpath:application.properties")
390+
@EnableStormpathWebSecurity
391+
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
392+
@Override
393+
protected void configure(HttpSecurity http) throws Exception {
394+
395+
http
396+
.apply(stormpath())
397+
.and()
398+
.authorizeRequests()
399+
.antMatchers("/").permitAll()
400+
.and()
401+
.exceptionHandling().accessDeniedPage("/403");
402+
}
403+
}
404+
#end
405+
406+
In order to easily hook into the Stormpath Spring Security integration, simply add a ``WebSecurityConfigurerAdapter`` in the application.
407+
#if( $spring )
408+
Then, apply stormpath using ``.apply(stormpath())``.
409+
#end
410+
Doing that just sets up all of the default views and hooks the Stormpath ``AuthenticationManager`` into your application.
379411

380412
Based on the ``SpringSecurityWebAppConfig`` above, we will permit access to the homepage. Any other paths will fall back
381413
to the default of being secured - you would be redirected to the Stormpath login page. We are going to further protect
@@ -407,22 +439,36 @@ NOTE: In this example, ``hasAuthority`` is used because Spring Security looks fo
407439
For this reason, we recommend you use ``hasAuthority``. See `this issue <https://github.com/stormpath/stormpath-sdk-java/issues/325#issuecomment-220923162>`_
408440
for more information.
409441

410-
If the authenticated user is not in the specified group, a ``403`` (forbidden) status will be returned. This will
411-
automatically redirect to ``/error``, which gets handled by our ``RestrictedErrorController.java``.
442+
If the authenticated user is not in the specified group, a ``403`` (forbidden) status will be returned.
443+
444+
#if( $springboot )
445+
This will automatically redirect to ``/error``, which gets handled by our ``RestrictedErrorController.java``.
412446
This returns a nicely formatted Thymeleaf template.
447+
#elseif( $spring)
448+
This will automatically redirect to ``/403``, which gets handled by our ``ErrorController.java``.
449+
This returns a nicely formatted JSP.
450+
#end
413451

414452
With the service defined, we can incorporate it into our controller, ``HelloController.java``:
415453

416454
.. code-block:: java
417455
:linenos:
418-
:emphasize-lines: 4,5,15-17
456+
:emphasize-lines: 7, 23-25
419457
420458
@Controller
421459
public class HelloController {
422460
423-
@Autowired
461+
private AccountResolver accountResolver;
424462
private HelloService helloService;
425463
464+
@Autowired
465+
public HelloController(AccountResolver accountResolver, HelloService helloService) {
466+
Assert.notNull(accountResolver);
467+
Assert.notNull(helloService);
468+
this.accountResolver = accountResolver;
469+
this.helloService = helloService;
470+
}
471+
426472
@RequestMapping("/")
427473
String home(HttpServletRequest req, Model model) {
428474
model.addAttribute("status", req.getParameter("status"));
@@ -432,25 +478,32 @@ With the service defined, we can incorporate it into our controller, ``HelloCont
432478
@RequestMapping("/restricted")
433479
String restricted(HttpServletRequest req, Model model) {
434480
String msg = helloService.sayHello(
435-
AccountResolver.INSTANCE.getAccount(req)
481+
accountResolver.getAccount(req)
436482
);
437483
model.addAttribute("msg", msg);
438484
return "restricted";
439485
}
440-
441486
}
442487
443-
Lines 4 and 5 use the Spring Autowiring capability to make the ``HelloService`` available in the ``HelloController``.
488+
Line 7 uses the Spring Autowiring capability to make the ``AccountResolver`` and the ``HelloService`` available in the
489+
``HelloController``.
444490

445-
Lines 15 - 17 attempt to call the ``sayHello`` method.
491+
Lines 23 - 25 attempts to call the ``sayHello`` method.
446492

447493
Give this a spin yourself. Make sure that you replace the ``MY_GROUP`` value in ``HelloService`` with the actual URL to the group you've
448494
setup in the Stormpath Admin Console.
449495

496+
#if( $springboot )
450497
.. code-block:: bash
451498
452499
mvn clean package
453500
mvn spring-boot:run
501+
#elseif( $spring )
502+
.. code-block:: bash
503+
504+
mvn clean package
505+
mvn tomcat7:run
506+
#end
454507

455508
In the next section, we'll add a small amount of code to be able to dynamically set the Group reference and make the code more readable.
456509

@@ -825,4 +878,6 @@ for more information on all that the Stormpath Java SDK has to offer.
825878
.. _tutorials/spring-boot/00-the-basics: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/00-the-basics
826879
.. _tutorials/spring/00-the-basics: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/00-the-basics
827880
.. _tutorials/spring-boot/01-some-access-controls: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/01-some-access-controls
828-
.. _tutorials/spring/01-some-access-controls: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/01-some-access-controls
881+
.. _tutorials/spring/01-some-access-controls: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/01-some-access-controls
882+
.. _tutorials/spring-boot/02-spring-security-ftw: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/02-spring-security-ftw
883+
.. _tutorials/spring/02-spring-security-ftw: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/02-spring-security-ftw

0 commit comments

Comments
 (0)