CSS拒绝使用Springboot / Spring Security / Thymeleaf

时间:2018-09-25 20:48:14

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

我精确地说我是Java Developper一年级的法国学生。 我知道这个话题已经解决了,但是我在网上看到的所有离线解决方案都对我有用。

CSS文件在我的webapp(多模块)中不起作用。 显然有些东西我看不到。自3天以来,我一直在努力解决这个问题...

如果有人可以看一下我的代码,我将不胜感激...

如果有人需要,my github repo(该链接应该在我的功能分支上。)

我在webapp层中的pom文件:

//...
<dependencies>

        <!-- =-=-=-= Module =-=-=-= -->
        <dependency>
            <groupId>org.thibaut</groupId>
            <artifactId>model</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thibaut</groupId>
            <artifactId>business</artifactId>
        </dependency>

        <!-- =-=-=-= Framework =-=-=-= -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
        </dependency>
//...

我的Thymeleaf视图:

    <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

    <head>

        <link rel="stylesheet"
        href="/webjars/bootstrap/4.1.3/css/bootstrap.min.css" />
        <script src="/webjars/bootstrap/4.1.3/js/bootstrap.min.js"></script>
        <script src="/webjars/jquery/3.3.1-1/jquery.min.js"></script>

        <link rel="stylesheet" type="text/css" media="all"
              th:href="@{/css/atlas.css}" href="../static/css/atlas.css"/>

    </head>

    <body>

    <div class="container">
            <table class="table table-bordered table-striped table-condensed">
                <caption>
                    <h3>LIST OF ALL ATLASES</h3>
                </caption>
                <thead>
                    <tr>
                        <th>NAME</th>
                        <th>AVAILABLE</th>
                        <th>CREATION DATE</th>
                        <th>OWNER</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="atlas:${atlases}">
                        <td th:text="${atlas.name}"></td>
                        <td th:text="${atlas.available}"></td>
                        <td th:text="${atlas.createDate}"></td>
                        <td th:text="${atlas.user.userName}"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>

</html>

我的WebSecurityConfig类:

@Configuration
//@EnableWebSecurity
//--> I saw somewhere that this annotation can be problematic with spring security and CSS
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Autowired
    private DataSource dataSource;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        // Setting Service to find User in the database.
        // And Setting PassswordEncoder
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());

    }

    @Override
    public void configure( WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring()
                // ignore all URLs that start with /resources/ or /static/
                .antMatchers("/resources/**", "/css/**");
    }

    @Override
    protected void configure( HttpSecurity http ) throws Exception {

        http.csrf().disable();

        // /userInfo page requires login as ROLE_USER or ROLE_ADMIN.
        // If no login, it will redirect to /login page.
        http.authorizeRequests().antMatchers(
                "/userInfo")
                .access("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')");

        // For ADMIN only.
        http.authorizeRequests().antMatchers(
                "/admin")
                .access("hasRole('ROLE_ADMIN')");

        // When the user has logged in as XX.
        // But access a page that requires role YY,
        // AccessDeniedException will be thrown.
        http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");

        // Config for Login Form
        http.authorizeRequests().and().formLogin()//
                // Submit URL of login page.
                .loginProcessingUrl("/j_spring_security_check") // Submit URL
                .loginPage("/login")//
                .defaultSuccessUrl("/userInfo")//
                .failureUrl("/login?error=true")//
                .usernameParameter("username")//
                .passwordParameter("password")
                //Config for Logout Page
                .and().logout().logoutUrl("/logout").logoutSuccessUrl("/logoutSuccessful");

        // The pages does not require login
        http.authorizeRequests().antMatchers(
                "/",
                "/login",
                "/css/**", //--> I also read that this should work on its own.. but of course, didn't work for me
                "/register",
                "/registerSuccessful",
                "/index",
                "/atlas").permitAll();
    }

}

我的WebConfiguration类:

@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        // Load file: validation.properties
        messageSource.setBasename("classpath:validation");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Override
    public void addResourceHandlers( ResourceHandlerRegistry registry) {
//      registry
//              .addResourceHandler("/resources/**")
//              .addResourceLocations("classpath:/static/");
//--> I tried that, but doesn't work
        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("/webjars/");
    }
}

还有我非常简单的atlas.css文件:

caption {
    padding: 10px;
    caption-side: top;
}


h3{
    color: darkorange;
}

body {
    padding-top: 10px;
    background: red;
}

所以,我希望大家看到一些巨大的错误后能对我放纵。 我真的想亲自在网上解决它,但是没有办法使它起作用...

感谢很多人!

1 个答案:

答案 0 :(得分:0)

您可以这样覆盖addResourceHandlers的{​​{1}}方法

WebMvcConfigurerAdapter

,并将 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } 称为:

atlas.css

假设您的<link rel="stylesheet" type="text/css" media="all" th:href="@{/static/css/atlas.css}"/> atlas.css下。