0

Tell me why I can't connect the css files in my index.html. enter image description here

I have tried various methods, but nothing has worked. In my project Spring Security...maybe it somehow affects.

My 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

<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...

2
  • but I need him for the future. Commented May 8, 2021 at 19:58
  • Are you using the @EnableWebMvc annotation? If so, you might want to try removing it as a test. Or try {styles/main_styles... Commented May 8, 2021 at 20:02

1 Answer 1

1

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}

also just to make sure it isn't spring security blocking it, I always added following in my security config:

 @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. 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

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response, you are helping me a fool for the second time =) Did everything as you said, but the styles still didn't apply. P.S. I didn't know about static, thanks
check out this for reference - it works for me github.com/asgarov1/springSecurityDemo
finally it worked =) This line helped me web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/vendor/**","/fonts/**").anyRequest(); And before that I wrote only this web.ignoring().antMatchers("static/**"); Thank you very much for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.