我有一个项目,我正在努力。我的资源有问题。我使用spring Security,但我无法访问用于制作html页面的资源。有人能帮助我吗? 这是我的资源: here
这是我的SpringSecurityConfig
<!DOCTYPE html>
<html lang="en" xmlns:th="http:thymeleaf.org">
<head>
<title>Events & People</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./styling.css">
</head>
package com.example.app.Configuration;
import com.example.app.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserService userService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(this.userService).passwordEncoder(getBCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests()
.antMatchers("/","/register", "/login").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/user/**").access("hasRole('USER') OR hasRole('ADMIN')")
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.and()
.rememberMe()
.rememberMeCookieName("TheBoardFinders")
.rememberMeParameter("remember")
.key("1random%2316secretcryptoTUES")
.tokenValiditySeconds(2629743)
.and()
.logout().logoutSuccessUrl("/login?logout").permitAll()
.and()
.exceptionHandling().accessDeniedPage("/unauthorized")
.and()
.csrf().disable();
}
@Bean
public BCryptPasswordEncoder getBCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
}
请不要将其标记为重复,因为我在这里搜索过,我发现了一些类似我的问题,但他们无法帮助我。
答案 0 :(得分:0)
将此方法添加到实施deferToThread
@org.springframework.context.annotation.Configuration
班级
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
并在您的@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry
.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/static/");
}
中,
将SpringSecurityConfig
更改为.antMatchers("/css/**").permitAll()
或将.antMatchers("/resources/**").permitAll()
更改为.anyRequest().authenticated()
。
您可以在此网址上访问您的(例如)css文件:
.anyRequest().permit()