简体   繁体   English

在我的基于 Spring 启动的项目中不连接 css 文件

[英]Doesn't connect css files in my project based Spring boot

Tell me why I can't connect the css files in my index.html.告诉我为什么我无法连接我的 index.html 中的 css 文件。 在此处输入图像描述

I have tried various methods, but nothing has worked.我尝试了各种方法,但没有任何效果。 In my project Spring Security...maybe it somehow affects.在我的项目 Spring 安全性......也许它会以某种方式影响。

My WebSecurityConfig class我的 WebSecurityConfig class

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  // @Autowired
 //  UserService userService;

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

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            //Доступ только для не зарегистрированных пользователей
            .antMatchers("/","/index","/login","/registration").permitAll()
            //Доступ только для пользователей с ролью Администратор
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/news").hasRole("USER")
            //Доступ разрешен всем пользователей
            .antMatchers("/", "/resources/**").permitAll()
            //Все остальные страницы требуют аутентификации
            .anyRequest().authenticated()
            .and()
            //Настройка для входа в систему
            .formLogin()
            .loginPage("/login")
            //Перенарпавление на главную страницу после успешного входа
            .defaultSuccessUrl("/")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .logoutSuccessUrl("/");
}

And this is how I connected in various ways in index.html这就是我在 index.html 中以各种方式连接的方式

<link rel="stylesheet" type="text/css" href="../../resources/styles/bootstrap-4.1.2/bootstrap.min.css">
<link href="../../resources/plugins/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../../resources/plugins/OwlCarousel2-2.3.4/owl.carousel.css">
<link rel="stylesheet" type="text/css" href="../../resources/plugins/OwlCarousel2-2.3.4/owl.theme.default.css">
<link rel="stylesheet" type="text/css" href="../resources/plugins/OwlCarousel2-2.3.4/animate.css">
<link rel="stylesheet" type="text/css" th:href="@{../resources/styles/main_styles.css}">
<link rel="stylesheet" type="text/css" th:href="@{../resources/styles/responsive.css}">

I don't understand why it doesn't connect...不明白为什么连不上。。。

You shouldn't use resources in href, and by convention you put static files like css, js and such into resources/static/ and then use th:href="@{styles/main_styles.css}您不应该在 href 中使用资源,按照惯例,您将 static 文件(如 css、js 等)放入resources/static/然后使用th:href="@{styles/main_styles.css}

also just to make sure it isn't spring security blocking it, I always added following in my security config:也只是为了确保它不是 spring 安全阻止它,我总是在我的安全配置中添加以下内容:

 @Override
 public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/vendor/**","/fonts/**").anyRequest(); 
 }

ps: Btw I couldnt help but notice - dnt use package names in plural - convention is singular so not controllers but controller . ps:顺便说一句,我忍不住注意到 - dnt 使用复数形式的 package 名称 - 约定是单数的,所以不是controllers ,而是controller You have to think that package name will be read as a whole line com.sportify.Sportify.controllers.HomeController which will be then used in import statements您必须认为 package 名称将被读取为整行com.sportify.Sportify.controllers.HomeController然后将在导入语句中使用

For the same reason only class name starts with capital letter and you shouldn't repeat package names - so correct would be com.yoursuranme.sportify.controller package structure. For the same reason only class name starts with capital letter and you shouldn't repeat package names - so correct would be com.yoursuranme.sportify.controller package structure. But this is just to provide info for future reference.但这只是为了提供信息以供将来参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM