无法从我的项目根目录访问静态build.html

时间:2016-12-14 19:07:42

标签: spring spring-mvc spring-boot spring-security maven-plugin

我正在尝试访问具有构建过程信息的静态HTML页面,并使用maven war插件生成它。但是,当我尝试访问项目根目录中生成的build.html时,没有任何内容显示或无法访问。但是,当我尝试访问src/main/webapp/static下的任何HTML文件时,我可以使用以下网址访问:http://localhost:8180/MyProject/static/about/build.html

但无法通过这种方式访问​​,我可能正在寻找:http://localhost:8180/MyProject/build.html

MyProject.war中的结构

MyProject.war

META-IN

static

WEB-INF

build.html

Maven War Plugin

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>src/main/webapp/static/about</directory>
                                <filtering>true</filtering>
                            </resource>
                        </webResources>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>

WebConfiguration类(web.xml)

public class AppWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppWebConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

AppWebConfiguration(Spring配置)

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackages")
public class AppWebConfiguration extends WebMvcConfigurerAdapter {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");

    }


    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

Spring安全配置

 @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
            .authorizeRequests()
            .antMatchers("/login").anonymous()
            .antMatchers("/", "/home").hasRole(ADMIN)
            .antMatchers("/admin/**").hasRole(ADMIN)

            .and().formLogin().loginPage("/login")
            .usernameParameter("ssoId").passwordParameter("password")
            .and().csrf()
            .and().exceptionHandling().accessDeniedPage("/errorpage");  

        }

请建议。提前谢谢!

1 个答案:

答案 0 :(得分:0)

registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/");