gpt4 book ai didi

java - 无法在 html 页面中加载 Css 文件

转载 作者:行者123 更新时间:2023-11-27 23:45:25 24 4
gpt4 key购买 nike

我正在尝试使用 spring security 创建一个登录页面,并在 AntMatcher 列表中添加了 .css 文件以获得许可。但是,我在尝试加载 CSS 文件时仍然收到以下错误。

异常:

home:6 GET http://localhost:8080/defaultStyle.css net::ERR_ABORTED 404

使用更改后的类进行更新。

WebSecurityConfig类代码:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity( jsr250Enabled = true,
prePostEnabled = true )
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{

@Autowired
CustomUserDetailsService customUserDetailsService;

@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;

@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;

@Autowired
private CustomAccessDeniedHandler customAccessDeniedHandler;

@Bean
public AuthenticationManager authenticationManagerBean() throws Exception
{
return super.authenticationManagerBean();
}

@Override
public void configure( WebSecurity web ) throws Exception
{
web.ignoring()
.antMatchers( "/*.css" ) // or better ending with ".{js,html}" or something
.antMatchers( "/resources/static/**/*" );
}

@Override
public void configure( HttpSecurity http ) throws Exception
{

http.cors().and().csrf().disable().authorizeRequests()

.antMatchers( "/*", "/admin", "/api/auth/**",
"/static/**",
"/css/*.css",
"/img/**",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/webjars/**" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( "/home" )
.loginProcessingUrl( "/login-user" )
.defaultSuccessUrl( "/dashboard" ).permitAll()
.and()
.logout().invalidateHttpSession( true )
.clearAuthentication( true )
.logoutRequestMatcher( new AntPathRequestMatcher( "/logout" ) )
.logoutSuccessUrl( "/login?logout" )
.permitAll().and()

.exceptionHandling().accessDeniedHandler( customAccessDeniedHandler ).authenticationEntryPoint( unauthorizedHandler ).and()
.sessionManagement().sessionCreationPolicy( SessionCreationPolicy.STATELESS );
http

.addFilterBefore( jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class );
}

@Autowired
public void globalUserDetails( AuthenticationManagerBuilder auth ) throws Exception
{
auth.userDetailsService( customUserDetailsService )
.passwordEncoder( passwordEncoder() );
}

@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
}

网络配置类

Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer
{

@Override
public void addResourceHandlers( ResourceHandlerRegistry registry )
{
registry
.addResourceHandler( "swagger-ui.html" )
.addResourceLocations( "classpath:/META-INF/resources/" );

registry
.addResourceHandler( "/webjars/**" )
.addResourceLocations( "classpath:/META-INF/resources/webjars/" );
registry
.addResourceHandler( "/static/**" )
.addResourceLocations( "classpath:/static/" );
registry.addResourceHandler( "/css/**" )
.addResourceLocations( "/css/" );
registry.addResourceHandler( "/img/**" )
.addResourceLocations( "/img/" );
registry.addResourceHandler( "/js/**" )
.addResourceLocations( "/js/" );
}

@Override
public void addCorsMappings( CorsRegistry registry )
{
registry.addMapping( "/**" )
.allowedMethods( "POST", "GET", "PUT", "OPTIONS", "DELETE" )
.allowedHeaders( "X-Auth-Token", "Content-Type" )
.exposedHeaders( "custom-header1", "custom-header2" )
.allowCredentials( false )
.maxAge( 4800 );
}

@Override
public void addViewControllers( ViewControllerRegistry registry )
{
System.out.println( "Inside MvcConfig addViewControllers() adding View forgot" );
registry.addViewController( "/admin/home" ).setViewName( "jsp/home" );
registry.addViewController( "/login" ).setViewName( "/login" );
}
}

enter image description here

新错误

enter image description here

编辑

我已经用修改后的网络安全文件更新了问题,并将异常更改为我现在得到的新异常。

最佳答案

请尝试下面的代码

 http.cors().and().csrf().disable().
authorizeRequests()
.antMatchers( "/",
"/static/**",
"/img/**",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/webjars/**" ).permitAll()
.antMatchers( "/api/auth/**" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( "/home" )
.loginProcessingUrl("/login")
.defaultSuccessUrl( "/dashboard").permitAll()
.and()

Ref 1 Ref 2

关于java - 无法在 html 页面中加载 Css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com