Java 不';t在基于项目的Spring boot中连接css文件
告诉我为什么无法连接index.html中的css文件 我试过各种方法,但都不管用。 在我的项目“春季安全”中……也许它会以某种方式影响 我的Web安全配置类Java 不';t在基于项目的Spring boot中连接css文件,java,css,spring,spring-boot,spring-security,Java,Css,Spring,Spring Boot,Spring Security,告诉我为什么无法连接index.html中的css文件 我试过各种方法,但都不管用。 在我的项目“春季安全”中……也许它会以某种方式影响 我的Web安全配置类 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // @Autowired
@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 security阻止它,我总是在我的安全配置中添加以下内容:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/vendor/**","/fonts/**").anyRequest();
}
附言:
顺便说一句,我忍不住要注意-dnt使用复数形式的包名-约定是单数的,所以不是controllers
,而是controllers
。您必须认为包名将被理解为一整行com.sportify.sportify.controllers.HomeController
,然后在导入语句中使用
出于同样的原因,只有类名以大写字母开头,您不应该重复包名-正确的应该是
com.yoursurame.sportify.controller
包结构。但这只是为了提供信息供将来参考。但我需要他为将来服务。您是否使用@enablewbmvc注释?如果是,您可能需要尝试删除它作为测试。或者尝试{styles/main_styles…谢谢你的回复,你第二次帮了我一个傻瓜=)按照你说的做了一切,但是样式仍然不适用。另外,我不知道静态,谢谢把这个拿出来作为参考-它对我有效最后它有效=)这一行帮助了我。忽略().antMatchers(“/resources/**”,“/static/**”,“/css/**”,“/js/**”,“/images/**”,“/vendor/**”,“/fonts/**”)。anyRequest();在此之前,我只写了这个web.ignering().antMatchers(“static/**”);非常感谢您抽出时间。