我正在尝试构建我的第一个Spring Web项目,但是我不想使用jsp。我只想使用rest控制器,但是我什至无法传递使用欢迎文件index.html(默认情况下为index.jsp)的第一个request(“ /”)。 我将粘贴我的文件夹结构和类。
这是我的应用程序配置类
package com.abs.sstest.security;
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
}
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
这是我的Init课
package com.abs.sstest.security;
public class AppInit implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
registerDispatcherServlet(servletContext);
}
private void registerDispatcherServlet(final ServletContext servletContext) {
WebApplicationContext dispatcherContext = createContext(AppConfig.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(annotatedClasses);
return context;
}
}
这是我的securityconfig类
package com.abs.sstest.security;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("bill").password("abc123").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("root123").roles("ADMIN");
auth.inMemoryAuthentication().withUser("dba").password("root123").roles("ADMIN","DBA");
}
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/","/home").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.and().formLogin()
.and().exceptionHandling().accessDeniedPage("/Access_Denied");
}
}
这是我的安全性初始化
package com.abs.sstest.security;
public class SecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {
}
这是我的文件夹结构文件夹结构
我在多个文件夹中粘贴了index.html,因为我在阅读各种论坛时感到困惑。
编辑: 现在我更改了我的应用程序配置类
package com.abs.sstest.security;
@Configuration
@ComponentScan(basePackages = "com.abs.sstest")
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/index");
registry.addRedirectViewController("/login2", "/login2");
}
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/views/**").addResourceLocations("/WEB-INF/views/");
}
}
init类现在更加简单
package com.websystique.springsecurity.configuration;
public class SpringMvcInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { HelloWorldConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
现在主要的是,我所有的html文件都位于src / main / webapp / index.html
但是这导致了一个新问题,现在我必须从控制器返回甚至扩展名。 例如:
@RequestMapping(value = "/login2", method = RequestMethod.GET)
public String loginPage() {
return "login.html";//login doesn't work
}
答案 0 :(得分:0)
首先,您应该将index.html放在/ WEB_INF / view /中。 看来您需要添加ViewResolver,以便Spring知道如何解析控制器返回的字符串。因此,请尝试将其添加到您的AppConfig类中:
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".html");
return bean;
}
当然,请确保您的pom文件中有必要的依赖项!