春季启动时使用404静态图片

时间:2019-07-03 09:17:31

标签: spring-boot

html文件中,有一个图像:

        <div class="login-branding">
        <a href="index.html"><img src="../static/images/logo.png" alt="Clevex" title="Clevex"></a>
    </div>

http://localhost:8090/login的地址,我看到控制台,看到:

  

获取http://localhost:8090/static/images/logo.png 404

但是当应用程序启动时,如果我去

http://localhost:8090/images/logo.png

此网址,我可以在chrome浏览器的控制台上看到图像而没有任何错误或警告。

这是我的安全配置:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationProvider customAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

 http.cors().and().csrf().disable();

        String[] resources = new String[]{
                "/",
                "/home",
                "/pictureCheckCode",
                "/include/**",
                "/css/**",
                "/icons/**",
                "/images/**",
                "/js/**",
                "/resources/**",
                "/layer/**",
                "/static/**",
                "/js/**",
                "/img/**",
                "/webjars/**"
        };

   http
                .authorizeRequests()

                .antMatchers(resources).permitAll()

我还添加了@EnableAutoConfiguration,但是它什么也没做。并给出了错误。

我不想使用addResourceHandlers,因为它是Spring Boot,它应该进行自动配置。

目录如下:

src
-main
-resources
--static
---images
----logo.png

这是应用程序启动的方式:

@SpringBootApplication
public class InternalApplication {

    public static void main(String[] args) {
        SpringApplication.run(InternalApplication.class, args);
    }

}

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

static目录中的所有内容都在Web应用程序的根目录中提供。因此/static/images/logo.png由Spring Boot的/images/logo.png提供服务。

所以替换:

src="../static/images/logo.png"

具有:

src="/images/logo.png"

答案 1 :(得分:0)

这有效:

https://stackoverflow.com/a/56196207/11369236

我改变了

<a href="index.html"><img src="../static/images/logo.png" alt="Clevex" title="Clevex"></a>

            <a href="index.html"><img src="/images/logo.png" alt="Clevex" title="Clevex"></a>

由于Intellij Idea,我感到困惑。

HTML代码中,如果我单击../static/images/logo.png,它可以转到图像。

但是当我在HTML代码中单击/images/logo.png时,它却没有显示,sonar也为此发出了警告。