我有一個Spring MVC的休息Web應用程序爲我在加入春季安全的層的過程很。添加Spring Security以現有的Spring Web應用程序(使用JavaConfig)
雖然我經歷了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(這是爲了測試在內存中使用auth的細節)。
@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+登錄。但我在春季安全方面正在採取寶貝步驟,我被困在這裏。想知道是否有誰已經採取這條道路已經可以分享他們的智慧。
你用這個成功了嗎? –
@最終用戶這已經有一段時間了,所以我不記得我確實做了什麼來解決它,但這裏是當前工作配置作爲參考https://github.com/billrive/billrive/tree/master/billrive- app/src/main/java/com/uhsarp/billrive/spring – user6123723