【问题标题】:spring boot security static resourcesspring boot 安全静态资源
【发布时间】:2017-03-29 20:48:02
【问题描述】:

我在 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

我在模板/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);
        }

    }

【问题讨论】:

  • 只是为了确认一下,整个页面是返回 404 还是只返回图片资源?
  • 404 只返回一个图像资产。其他内容加载成功。

标签: spring-mvc spring-security spring-boot thymeleaf wro4j


【解决方案1】:

这就是我在 Spring Boot 中做静态资源的方式,在你的 WebConfig 类或扩展 WebMvcConfigurerAdapter 的类中,添加这个:

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

然后,在resources 中创建一个static 文件夹,您可以在其中放置所有静态文件或文件夹,例如resources/cssresources/js

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

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

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

.antMatchers("/resources/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,问题在于 Spring Security,因此任何遇到此问题的人都可以尝试将其静态文件所在的文件夹添加到不需要身份验证的 URL 列表中 例如antMatchers("/css/**", "/fonts/**").permitAll()

    【讨论】:

      【解决方案3】:

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

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

      /praca.jpg

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        相关资源
        最近更新 更多