【问题标题】:Spring Security Thymleaf static resources don't loadSpring Security Thymeleaf 静态资源不加载
【发布时间】:2016-08-17 00:19:29
【问题描述】:

我正在使用带有 Thymleaf 和 Spring-Security 的 SpringMVC。 我想使用 Thymleaf 模板加载页面,并且可以加载我的静态资源。

例如,我想从 template.html 加载位于:static/img/theme/logo.png 的图片

这是我所拥有的:result


模板.html:

<body>
    <div layout:fragment="content">
                
        <a href="">img src="../static/img/theme/logo.png" alt="Logo"></a>
                                                        
        <h1>Hello</h1>
                  
    </div>
        
</body>

MvcConfig.java

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/template").setViewName("template");
        registry.addViewController("/layout").setViewName("layout");
        registry.addViewController("/login").setViewName("login");
        
    }
    
   
    
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
  
  
}

网络安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    
    //List of all free pages
    
    private static final String[] pagesFree = {
            "/home",
            "/template",
            "/layout",
            
            //Thymleaf directory
            "/css/**",
            "/js/**",
            "/img/**",
            "/fonts/**",
            "/ico/**",
            "/twitter/**",
            "/"
            };
    
    
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        
        
        
        http
            .authorizeRequests()
                .antMatchers(pagesFree).permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("u").password("u").roles("USER");
    }
    
  
}

Source Code tree

【问题讨论】:

    标签: spring spring-security static thymeleaf


    【解决方案1】:

    在您的安全配置中,您可以声明如下内容:

    /** Public URLs. */
    private static final String[] PUBLIC_MATCHERS = {
            "/webjars/**",
            "/css/**",
            "/js/**",
            "/images/**",
            "/"
    };
    

    然后是这样的:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        if (activeProfiles.contains("dev")) {
            http.csrf().disable();
            http.headers().frameOptions().disable();
        }
    
        http
                .authorizeRequests()
                .antMatchers(PUBLIC_MATCHERS).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
                .failureUrl("/login?error").permitAll()
                .and()
                .logout().permitAll();
    }
    

    在您的 Thymeleaf 模板中,您会声明如下内容:

    <img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />
    

    您的项目的工作副本可以在here找到。

    【讨论】:

    • 我有相同的安全配置,除了我暂时不管理我的个人资料。而当我把这条线来获取图像时:
      &lt;a href=""&gt;&lt;img th:src="@{/img/theme/logo.png}" alt="Logo"&gt;&lt;/a&gt;
      我仍然没有到达图像。
    • 您好,我做了一些更改。你可以从这里从 GitHub 下载:github.com/mtedone/nickeed 模板页面现在带有徽标
    • 嗨,我试过了,一切都很好!非常感谢:)
    • 我的荣幸。顺便说一句,我正在完成关于如何使用 Thymeleaf、Bootstrap、Spring Boot、Email、Data JPA、Security 和 Amazon Web Services 从头开始​​创建 Spring Boot 网站的在线课程的最后几堂课。如果您愿意,可以在这里注册您的兴趣:academy.devopsfolks.com/courses/…
    猜你喜欢
    • 2017-11-02
    • 2015-03-08
    • 2018-09-11
    • 2017-07-26
    • 2015-08-21
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多