在基于 Spring 启动的项目中不连接 css 文件
Doesn't connect css files in my project based Spring boot
告诉我为什么我无法连接 index.html 中的 css 个文件。
我尝试了各种方法,但没有任何效果。
在我的项目中 Spring 安全...也许它会以某种方式影响。
我的 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("/");
}
这就是我在 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}">
不明白为什么连接不上...
你不应该在 href 中使用资源,按照惯例你将 css、js 等静态文件放入 resources/static/
,然后使用 th:href="@{styles/main_styles.css}
也只是为了确保它不是 spring 安全阻止它,我总是在我的安全配置中添加以下内容:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/vendor/**","/fonts/**").anyRequest();
}
ps:
顺便说一句,我忍不住注意到 - dnt 使用复数形式的包名称 - 约定是单数的,所以不是 controllers
而是 controller
。您必须认为包名称将作为整行 com.sportify.Sportify.controllers.HomeController
读取,然后将在导入语句中使用
出于同样的原因,只有 class 名称以大写字母开头,您不应该重复包名称 - com.yoursuranme.sportify.controller
包结构是正确的。但这只是提供信息以供将来参考。
告诉我为什么我无法连接 index.html 中的 css 个文件。
我尝试了各种方法,但没有任何效果。 在我的项目中 Spring 安全...也许它会以某种方式影响。
我的 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("/");
}
这就是我在 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}">
不明白为什么连接不上...
你不应该在 href 中使用资源,按照惯例你将 css、js 等静态文件放入 resources/static/
,然后使用 th:href="@{styles/main_styles.css}
也只是为了确保它不是 spring 安全阻止它,我总是在我的安全配置中添加以下内容:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/vendor/**","/fonts/**").anyRequest();
}
ps:
顺便说一句,我忍不住注意到 - dnt 使用复数形式的包名称 - 约定是单数的,所以不是 controllers
而是 controller
。您必须认为包名称将作为整行 com.sportify.Sportify.controllers.HomeController
读取,然后将在导入语句中使用
出于同样的原因,只有 class 名称以大写字母开头,您不应该重复包名称 - com.yoursuranme.sportify.controller
包结构是正确的。但这只是提供信息以供将来参考。