1

我在 Spring Boot 中编写应用程序,使用 Thymeleaf 编写 Spring Security 并尝试访问我的静态资源文件...

这是我的项目结构...

    .
    ├── mvnw
    ├── mvnw.cmd
    ├── nb-configuration.xml
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   ├── resources
    │   │   │   ├── application.properties
    │   │   │   ├── static
    |   |   |   |    |---------------------------------this is image.jpg
    │   │   │   ├── templates
    │   │   │   └── ValidationMessages.properties
    │   │   └── wro
    │   │       ├── css
    │   │       ├── fonts
    │   │       ├── js
    │   │       ├── scss
    │   │       ├── wro.properties
    │   │       └── wro.xml
    │   └── test
    │       └── java
    │           └── com

我在 templates/index.html 中有 HTML 文件,我尝试使用标签

     <img src="/praca.jpg" alt="sd"/>

为什么我总是收到 404 错误?我哪里做错了??

我的一般初始化类:

    @SpringBootApplication
    public class Application extends WebMvcConfigurerAdapter {

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

我的安全课:

    @Configuration
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Autowired
        private UserAuthenticationDetails userAuthenticationDetails;

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userAuthenticationDetails);
            auth.authenticationProvider(authenticationProvider());
        }

        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }

        @Bean
        public DaoAuthenticationProvider authenticationProvider() {
            DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
            authenticationProvider.setUserDetailsService(userAuthenticationDetails);
            authenticationProvider.setPasswordEncoder(passwordEncoder());
            return authenticationProvider;
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .authorizeRequests()
                    .antMatchers("/","/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .defaultSuccessUrl("/",true)
                    .and()
                    .logout()
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutUrl("/logout")
                    .logoutSuccessUrl("/login?logout")
                    .invalidateHttpSession(true);
        }

    }
4

3 回答 3

1

在您的模板中,您需要使用 thymeleaf 格式自行自动添加上下文。用这个:

<img th:src="@{/praca.jpg}" alt="sd"/>

/praca.jpg

应该是静态或公用文件夹中图像的完整路径

于 2016-11-16T13:28:49.927 回答
0

这就是我在 Spring Boot、您的WebConfig类或扩展类中处理静态资源的方式WebMvcConfigurerAdapter,添加以下内容:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

然后,在resources创建一个static文件夹中放置所有静态文件或文件夹,如resources/css,resources/js

从您的角度来看,您可以像这样访问它,例如:

 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/skin-black.css">

如果您使用的是 Spring Security,请确保添加antMatchers

.antMatchers("/resources/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
于 2018-08-04T19:17:06.370 回答
0

我有一个类似的问题,问题是弹簧安全性,所以任何有这个问题的人都可以尝试将其静态文件所在的文件夹添加到不需要身份验证的 URL 列表中,例如antMatchers("/css/**", "/fonts/**").permitAll()

于 2017-03-04T02:28:50.973 回答