簡體   English   中英

Spring Boot 1.4和Thymeleaf 2安全性

[英]Spring Boot 1.4 and Thymeleaf 2 Security

我正在嘗試Spring Boot 1.4和Thymeleaf2。我想保護我的應用程序。 這是我的安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/resources/static/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
}

這是我的MVC控制器:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/login").setViewName("login");
    }    
}

但是,當我嘗試訪問localhost:8080它顯示沒有css,js的索引頁! 當我通過localhost:8080/login.html手動登錄時,一切正常。 我遵守Spring-Thymeleaf集成的約定,靜態資源在/resources/static ,模板在/resource/templates

對我來說還有什么不足?

編輯:

它以這種方式工作:

    http
        .authorizeRequests()
            .antMatchers("/css/**").permitAll()
            .antMatchers("/js/**").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();

但是,是否應該通過這種方式將所有文件夾添加到靜態資源下? 看來我想念什么。

您應該將sss和javascript文件添加到src/main/webapp

現在給出的結構如下:

webapp
|__css/myStyles.css
|__js/myjs.js
|__images/myImage.gif
|__WEB-INF

將此添加到您的安全配置

@Override
public void configure(WebSecurity security) {
    security.ignoring().antMatchers("/css/**", "/fonts/**", "/libs/**", "/js/**", "/images/**");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM