【发布时间】:2014-03-07 00:38:45
【问题描述】:
我有一个 Spring MVC Rest Web App,我正在为其添加一个 Spring Security 层。
当我浏览Spring documentation 时,我无法理解第 3.1.3 节的含义。我正在复制/粘贴以下部分的内容。
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 };
}
// ... other overrides ...
}
所以,我已经有了以下内容
an Initializer.java (replacement of web.xml)
Config.java - Root Context
RestServlet.java - Servlet Context
这是我的 Initializer.java
public class Initializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(Config.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(RestServlet.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
为了添加 Spring Security 层,我添加了以下内容
SecurityConfig.java
SecurityInitializer.java
SecurityConfig.java(这是为了测试使用内存认证细节)。
@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,生成令牌并将其发布到前端 web 应用程序(基于 Angular),并以这种方式保护 REST api。还要在此基础上添加 Facebook 和 Google+ 登录。但是我正在采取弹簧安全措施,我被困在这里。想知道走这条路的人是否已经可以分享他们的智慧。
【问题讨论】:
-
你在这方面取得过成功吗?
-
@end-user 已经有一段时间了,所以我不记得我究竟做了什么来修复它,但这是当前工作配置作为参考github.com/billrive/billrive/tree/master/billrive-app/src/main/…
标签: spring rest spring-mvc spring-security spring-social