Java 春季安全-为什么可以';我是否访问WebSecurityConfigureAdapter中定义的静态资源?

Java 春季安全-为什么可以';我是否访问WebSecurityConfigureAdapter中定义的静态资源?,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我正在重构一个Spring Boot应用程序,我遇到了 o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/rootProject/css/app.css] 用于所有CSS和JavaScript文件 该项目的结构如下: 根项目 src 主要 爪哇 资源 css js 我的主要安全实现是: @Configuration @EnableWebSecurity public cla

我正在重构一个Spring Boot应用程序,我遇到了

o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/rootProject/css/app.css]
用于所有CSS和JavaScript文件

该项目的结构如下:

  • 根项目

    • src

      • 主要

        • 爪哇

        • 资源

          • css

          • js

我的主要安全实现是:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    // other methods

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // TODO: api must be protected but default alpaca connector does not support protected sources. We'll need to register a custom connector
        http
                .authorizeRequests()
                .antMatchers(
                        "/images/**",
                        "/css/**",
                        "/health/**",
                        "/js/**",
                        "/api/**",
                        "/reports/**",
                        "/h2-console/**",
                        "/oauth/authorize",
                        "/oauth/confirm_access",
                        "/oauth/token_key").permitAll()
                .antMatchers("/admin/**").hasAuthority(BaseRoleEnum.BASE_ADMIN.toString())
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login")
                .and()
                .headers()
                .frameOptions()
                .sameOrigin()
                .and()
                .csrf()
                .disable();
    }
}
我的主要配置类是:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "basePackages")
// Enable caching for the application
//@EnableCaching
public class TouchConfiguration extends WebMvcConfigurerAdapter{

    // other beans and methods

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
}

我在这里遗漏了什么,以便按预期工作?

我已将所有静态资源路径安全性添加为“无”。这是一个全局配置。下面的示例是XML格式的。。您也应该能够在config中执行同样的操作

<!-- Global Security Ignore for resources -->
    <security:http pattern="/resources/**" security="none" ></security:http>
    <security:http pattern="/*.css*" security="none" ></security:http>
    <security:http pattern="/*.js*" security="none" ></security:http>
    <security:http pattern="/*.gif*" security="none" ></security:http>
    <security:http pattern="/*.png*" security="none" ></security:http>
    <security:http pattern="/*.jpg*" security="none" ></security:http>
    <security:http pattern="/*.svg*" security="none" ></security:http>
    <security:http pattern="/*.ico*" security="none" ></security:http>

我认为在/resources/static/**路径下应该有.css和.js文件


另外,请确保您以正确的方式更正了链接

我已经这样做了!结构是
/main/resources/static/**
。它的结构与您的图像相同。如果像这样忽略对这些资源的请求@Override public void configure(final WebSecurity WebSecurity)会引发异常{WebSecurity.ignering().antMatchers(“/css/**”).antMatchers(“/js/**”).antMatchers(“/font/**”);},请确保您以我使用thymeleaf的正确方式更正了链接,因此我的链接看起来是这样的:
我想您可以试试这个我可以很好地使用我@Override public void addResourceHandlers(ResourceHandlerRegistry registry){registry.addResourceHandler(“/resources/**”).addResourceLocations(“classpath:/resources/”;)为什么
.addResourceLocations(“/static/”)在您提供的项目结构中没有这样的文件夹。另外,
/rootProject/css/app.css
是确切的消息还是您替换了
rootProject
部分?你的html css/js链接是什么?像这样的
因为这就是您在
.addResourceLocations(“/static/”)中所说的但错误显示不同的LinkedExact消息是:
警告o.s.web.servlet.PageNotFound-在名为“DispatchersServlet”的DispatchersServlet中找不到URI为[/central/css/app.css]的HTTP请求的映射和文件夹结构是否正确?您是否有
resources/static/css
或只是
resources/css
?是的,
resources/static/css
。请尝试
registry.addResourceHandler(“/central/**”).addResourceLocations(“/static/”