Switched H2 to server-mode and made the console available in the app
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / WebSecurityConfig.java
1 package de.juplo.yourshouter;
2
3 import javax.inject.Inject;
4 import org.springframework.context.annotation.Configuration;
5 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9 import org.springframework.security.web.AuthenticationEntryPoint;
10
11
12 @Configuration
13 @EnableWebSecurity
14 public class WebSecurityConfig extends WebSecurityConfigurerAdapter
15 {
16   @Inject
17   AuthenticationEntryPoint authenticationEntryPoint;
18
19
20   /**
21    * @{@inheritDoc}
22    *
23    * Override the default-implementation to configure the authentication
24    * mechanism of Spring Security.
25    * <ul>
26    * <li>
27    * We suppress the support of CSRF-tokens for our canvas-page, because
28    * the implementation consideres the initial call of Facebook to
29    * the canvas-page of our app as invalid, because it is issued as a post
30    * and the CSRF-token is missing.
31    * </li>
32    * <li>
33    * We inject our specialized implementation of the
34    * {@link AuthenticationEntryPoint}-interface.
35    * </li>
36    * <li>
37    * We configure the mechanism, that adds securtiy headers to the response,
38    * to disable the headers, that deny, to display our content insiede a frame,
39    * because otherwise, the browser would not render our content, when the
40    * app is displayed inside of Facebook through our canvas-page.
41    * </li>
42    * <li>
43    * Last but not least, we configure the pages, that should be accessible
44    * without authentication.
45    * </li>
46    * </ul>
47    */
48   @Override
49   protected void configure(HttpSecurity http) throws Exception
50   {
51     http
52         .csrf()
53             /**
54              * Neither the Facebook-Canvas nor the H2-console does send a proper
55              * CSRF-token in its POST-requests. Hence, this feature has to be
56              * disabled for this pages.
57              */
58             .ignoringAntMatchers("/canvas/*", "/h2-console/*")
59             .and()
60         .exceptionHandling()
61             .authenticationEntryPoint(authenticationEntryPoint)
62             .and()
63         .headers()
64             /**
65              * All pages must be allowed, to be displayed inside a frame.
66              * Otherwise, the content will not show up after a successfull
67              * login through the Facebook-Canvas, because it is shown inside
68              * a frame!
69              */
70             .frameOptions().disable()
71             .and()
72         .authorizeRequests()
73             .antMatchers("/signin.html", "/signin/*", "/canvas/*").permitAll()
74             .anyRequest().authenticated();
75   }
76
77   /**
78    * {@inheritDoc}
79    *
80    * Override the default-implementation, to configure Spring Security to use
81    * in-memory authentication.
82    */
83   @Override
84   public void configure(AuthenticationManagerBuilder auth)
85       throws
86         Exception
87   {
88     auth.inMemoryAuthentication();
89   }
90 }