【问题标题】:Spring security - Why can't I access static resources as defined in WebSecurityConfigurerAdapter?Spring security - 为什么我不能访问 WebSecurityConfigurerAdapter 中定义的静态资源?
【发布时间】:2017-09-19 14:59:37
【问题描述】:

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

o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/rootProject/css/app.css]

适用于所有 CSS 和 JavaScript 文件。

项目的结构如下:

  • 根项目

    • src

      • 主要

        • java

        • 资源

          • 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/");
    }
}

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

【问题讨论】:

  • 为什么.addResourceLocations("/static/");你提供的项目结构中没有这样的文件夹。还有/rootProject/css/app.css 是确切的消息还是你替换了rootProject 部分?你的 html css/js 链接是什么?像这样的东西? <link rel="stylesheet" type="text/css" href="/static/css/main.css"> 因为这就是你在.addResourceLocations("/static/"); 中所说的,但错误显示不同的链接
  • 确切消息是:WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet'
  • 和文件夹结构是否正确?你有resources/static/css 还是只有resources/css
  • 是的,resources/static/css
  • 试试registry.addResourceHandler("/central/**").addResourceLocations("/static/");

标签: java spring spring-mvc spring-security


【解决方案1】:

我已将所有静态资源路径安全性添加为无。这是一个全局配置。下面的例子是在 XML 中。你应该也可以在配置中做同样的事情。

<!-- 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>

【讨论】:

    【解决方案2】:

    我认为您应该在 /resourses/static/** 路径下有 .css 和 .js 文件 project static resources structure in elipse

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

    【讨论】:

    • 我有!结构是/main/resources/static/**。它与您的图像结构相同。
    • 如何忽略对这些资源的请求,例如 @Override public void configure(final WebSecurity webSecurity) throws Exception { webSecurity.ignoring() .antMatchers("/css/**") 。 antMatchers("/js/**") .antMatchers("/font/**"); }
    • 另外,请确保以正确的方式正确链接
    • 我正在使用百里香,所以我的链接如下所示:&lt;link rel="stylesheet" th:href="@{/static/css/app.css}"/&gt;
    • 我想你可以试试这个我工作得很好@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("classpath:/resources/ "); }
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2016-08-17
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2017-11-02
    • 2019-08-09
    • 2017-01-09
    相关资源
    最近更新 更多