【发布时间】:2015-09-04 19:46:29
【问题描述】:
我正在开发一个启动嵌入式 Jetty 服务器的 Spring 应用程序。然后它将 Spring MVC Web 应用程序“部署”到此 Jetty 服务器。
所有控制器都可以很好地使用多个控制器,但我无法将 Spring Security 添加到 Web 应用程序。 我使用编程和基于注释的配置,Jetty 服务器的配置如下:
Server server = new Server(8080);
server.setStopAtShutdown(true);
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.mypackage.web");
context.setParent(mainContext);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath("/");
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
DefaultServlet staticServlet = new DefaultServlet();
contextHandler.addServlet(new ServletHolder(dispatcherServlet), "/");
contextHandler.addServlet(new ServletHolder("staticServlet", staticServlet), "/res");
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase("webapp");
server.setHandler(contextHandler);
我还创建了一个扩展 WebSecurityConfigurerAdapter 的类 com.mypackage.web.SecurityConfig 并覆盖了 configure 方法,如下所示:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
}
据我了解文档,这应该足以“锁定”我的应用程序。当我在调试模式下启动应用程序时,在配置方法中遇到了一个断点,所以 Spring 似乎检测到了配置类。
但是,我仍然可以访问应用程序中的任何页面,而无需重定向到默认登录表单。
我是否需要告诉 Jetty Servlet 容器有关此配置的信息,或者我是否遗漏了其他内容?
【问题讨论】:
标签: spring spring-mvc spring-security jetty embedded-jetty