首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring boot安全静态资源

spring boot安全静态资源
EN

Stack Overflow用户
提问于 2016-11-16 04:12:15
回答 3查看 3.6K关注 0票数 1

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

这是我的项目结构..。

代码语言:javascript
复制
    .
    ├── 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

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

代码语言:javascript
复制
     <img src="/praca.jpg" alt="sd"/>

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

我的通用init类:

代码语言:javascript
复制
    @SpringBootApplication
    public class Application extends WebMvcConfigurerAdapter {

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

我的安全类:

代码语言:javascript
复制
    @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);
        }

    }
EN

回答 3

Stack Overflow用户

发布于 2016-11-16 21:28:50

在您的模板中,您需要使用胸腺叶格式自行自动添加上下文。使用以下命令:

代码语言:javascript
复制
<img th:src="@{/praca.jpg}" alt="sd"/>

/praca.jpg

应为静态或公共文件夹中的映像的完整路径

票数 1
EN

Stack Overflow用户

发布于 2017-03-04 10:28:51

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

票数 0
EN

Stack Overflow用户

发布于 2018-08-05 03:17:06

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

代码语言:javascript
复制
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

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

从您的视图中,您可以像这样访问它,例如:

代码语言:javascript
复制
 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/skin-black.css">

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

代码语言:javascript
复制
.antMatchers("/resources/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40618838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档