使用Spring Security后,我从template文件夹中的html页面停止显示。 我正在尝试使用Spring Security保护我的REstful API,并且我在资源文件夹下的templates文件夹中有一些静态html页面。
在实施Spring Security之前,所有页面都将显示并可以导航。 但是在实施Spring Security之后,所有页面都停止显示。
我尝试了几个线程,特别是这个线程与我的问题无关: After spring security implementation it's blocking all resources in my static folder?
并尝试在那里提供解决方案。但是那些解决方案并不能解决我的问题。使用这些解决方案后,我只能显示主页,而不能导航到其他页面。 我有个建议,以防万一模板中有一些静态html页面,那么这些源应该在application.propert文件中声明,并且此声明已为该项目正确完成。
下面是我的安全性配置类中的代码。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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;
import org.zevgma.api.auth.ManagementUserDetailsService;
@Configuration
@EnableWebSecurity
public class ApplicationsecutiryConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
ManagementUserDetailsService managementUserDetailsService;
@Bean
public DaoAuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(managementUserDetailsService);
provider.setPasswordEncoder(new BCryptPasswordEncoder());
return provider;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider( authenticationProvider());
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/assets/**", "/customs/**", "/templates/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/","/ru", "/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/assets/**","/fonts/**", "/**/favicon.ico" ,
"/ajax/**", "/bootstrap/**", "/templates/**").permitAll()
.anyRequest().authenticated();
}
}
我尝试遵循以下提到的线程的建议,但无法解决我的问题。 -Serving static web resources in Spring Boot & Spring Security application -https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot -Unable to load static content in spring security -Spring security does not allow CSS or JS resources to be loaded -Spring Boot not serving static content