我是Sping Boot 的新手,我正在创建我的新演示电子商务项目来练习使用Spring Boot和Thymeleaf
我面临的问题是试图链接引导CSS文件到HTML在登录页面,而没有身份验证
这里是我的html链接标签:
<link th:rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-2/css/bootstrap.min.css} "/>
字符串
我的pom.xml依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
型
我的Spring Security Config类:
@Configuration
public class SecurityConfig {
//bcrypt bean definition
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider(UserService userService) {
DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
auth.setUserDetailsService(userService); //set the custom user details service
auth.setPasswordEncoder(passwordEncoder()); //set the password encoder - bcrypt
return auth;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(configurer ->
configurer.requestMatchers("/register/**",
"/css/**",
"/js/**",
"/webjars/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(form ->
form
.loginPage("/showLoginPage")
.loginProcessingUrl("/authenticateUser")
.permitAll()
)
.logout(logout -> logout.permitAll()
)
.exceptionHandling(configurer ->
configurer.accessDeniedPage("/access-denied")
);
return http.build();
}
}
型
大多数解决方案使用 * authorizeRequests().antMatchers()**,这是不推荐的。此外,如果有人注意到任何其他错误,请指导我,因为我说我是新的Spring启动
1条答案
按热度按时间nlejzf6q1#
我发现问题不是用**@EnableWebSecurity注解 *SecurityConfig类。
下面是我的SecurityConfig类
字符串
我还将pom.xml更改为使用bootstrap 4.6.2
型
最后,这是我如何导入引导在thymeleaf html
型