I have a Spring MVC Rest Web web application for which I am in the process of adding Spring's security layer.
While I am looking through Spring documentation , I cannot understand the meaning of section 3.1.3. I copy / paste the contents of the section below.
If we were using Spring elsewhere in our application we probably already had a WebApplicationInitializer that is loading our Spring Configuration. If we use the previous configuration we would get an error. Instead, we should register Spring Security with the existing ApplicationContext. For example, if we were using Spring MVC our SecurityWebApplicationInitializer would look something like the following:
import org.springframework.security.web.context.*;
public class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {
}
This would simply only register the springSecurityFilterChain Filter for every URL in your application. After that we would ensure that SecurityConfig was loaded in our existing ApplicationInitializer. For example, if we were using Spring MVC it would be added in the getRootConfigClasses()
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SecurityConfig.class };
}
}
So I already have
an Initializer.java (replacement of web.xml)
Config.java - Root Context
RestServlet.java - Servlet Context
Here is my Initializer.java
public class Initializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(Config.class);
container.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(RestServlet.class);
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
To add Spring's security level, I added the following
SecurityConfig.java
SecurityInitializer.java
SecurityConfig.java (This must be verified using the data in memory).
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
SecurityInitializer.java
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer
{
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SecurityConfig.class };
}
, , . ( 3.2.3 ), AbstractSecurityWebApplicationInitializer AbstractAnnotationConfigDispatcherServletInitializer.
, REST. , jsps ( !). - OAuth2, frontend webapp ( Angular) REST api . Facebook Google+. Spring , . , - , , .