1

Thymeleaf を使用して Spring Boot、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

static フォルダーまたは public フォルダーからのイメージへのフル パスである必要があります。

于 2016-11-16T13:28:49.927 に答える