我正在开发一个启动嵌入式 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);
我还创建了一个类 com.mypackage.web.SecurityConfig 扩展WebSecurityConfigurerAdapter
并重写配置方法,如下所示:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
}
据我了解文档,这应该足以“锁定”我的应用程序。当我在调试模式下启动应用程序时,configure 方法中会遇到断点,因此 Spring 似乎检测到了配置类。
但是,我仍然可以访问应用程序中的任何页面,而无需重定向到默认登录表单。
我是否需要告诉 Jetty Servlet 容器有关此配置的信息,还是我遗漏了其他内容?