从XML切换到基于代码的Spring配置后,“Hello World!”成功登录后返回页面。为什么Spring无法映射控制器的方法?
的AppConfig
@Configuration
@ComponentScan(basePackages = "app.controller")
public class AppConfig {}
SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("a")
.password("a")
.roles("ADMIN");
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**"); // #3
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
// .antMatchers("/signup","/about").permitAll() // #4
.antMatchers("/**").hasRole("ADMIN") // #6
.anyRequest().authenticated() // 7
.and()
.formLogin() // #8
// .loginUrl("/login") // #9
.permitAll(); // #5
}
}
SecurityWebApplicationInitializer
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}
WebMvcConfig
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/pages/");
bean.setSuffix(".jsp");
return bean;
}
}
WebAppInitializer
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
rootContext.register(SecurityConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(WebMvcConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
AppController
@Controller
@RequestMapping("/")
public class AppController {
@RequestMapping(value = "/", method= RequestMethod.GET)
public String getRequestPage(Model model){
return "request";
}
我了解WebAppInitializer会链接AppConfig
,WebMvcConfig
和SecurityConfig
。 SecurityWebApplicationInitializer
似乎是孤独的阶级。 AppConfig
假设因AppController
而自动加载@ComponentScan
。正确?为什么不起作用?
更新
我index.jsp
WEB-INF
Hello World!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>.....</head>....</html>
。
的 UPDATE2
request.jsp
auth
UPDATE3
一句话:IDE强调Could not autowire. No beans of AuthenticationManagerBuilder type found
AuthenticationManagerBuilder
。index.jsp
。 Official Spring example不包含index.jsp
bean显式定义。为什么呢?
解决
我不知道为什么{{1}}会覆盖控制器映射。但是从WEB-INF中删除{{1}}解决了这个问题。它为什么会这样?
答案 0 :(得分:0)
我认为问题是你的AppConfig。在WebAppInitializer中,您将AppConfig类添加到&#34;根上下文&#34;。这意味着,您的控制器将被扫描并添加到&#34;根上下文&#34;但是其余的MVC配置(WebMvcConfig)将在Web应用程序上下文中加载(通过DispatcherServlet)。
事实上,您根本不需要App配置。您可以在WebMvcConfig类中添加@ComponentScan注释,如下所示:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "app.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
....
}
这应该让您的控制器被扫描并添加到Web应用程序上下文。