相关疑难解决方法(0)

在Spring Boot和Spring Security应用程序中提供静态Web资源

我正在尝试使用Spring安全性Java配置开发Spring Boot Web应用程序并保护它.

按照Spring博客中的建议将静态Web资源放入' src/main/resources/public '后,我就能获得静态资源.即在浏览器中点击确实提供html内容.https://localhost/test.html

问题

启用S​​pring Security后,点击静态资源URL需要进行身份验证.

我的相关Spring Security Java配置如下所示: -

@Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.
            authorizeRequests()
                .antMatchers("/","/public/**", "/resources/**","/resources/public/**")
                    .permitAll()
                .antMatchers("/google_oauth2_login").anonymous()
                    .anyRequest().authenticated()
                .and()
                .formLogin()
                    .loginPage("/")
                    .loginProcessingUrl("/login")
                    .defaultSuccessUrl("/home")
                    .and()
                    .csrf().disable()
                    .logout()
                        .logoutSuccessUrl("/")
                        .logoutUrl("/logout") // POST only
                .and()
                    .requiresChannel()
                    .anyRequest().requiresSecure()
                .and()
                    .addFilterAfter(oAuth2ClientContextFilter(),ExceptionTranslationFilter.class)
                    .addFilterAfter(googleOAuth2Filter(),OAuth2ClientContextFilter.class)
                .userDetailsService(userService);
        // @formatter:on
    }
Run Code Online (Sandbox Code Playgroud)

我应该如何配置antMatchers以允许放置在src/main/resources/public中的静态资源?

spring-mvc spring-security spring-boot

62
推荐指数
6
解决办法
10万
查看次数

如何在Spring Boot中将Cache-Control标头添加到静态资源?

如何Cache-Control在Spring Boot中为静态资源添加HTTP头?

尝试在应用程序中使用过滤器组件,它正确地写入标头,但Cache-Control标头被覆盖.

@Component
public class CacheBustingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
                                              throws IOException, ServletException {

        HttpServletResponse httpResp = (HttpServletResponse) resp;
        httpResp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("This-Header-Is-Set", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("Expires", "0");

        chain.doFilter(req, resp);
    }
Run Code Online (Sandbox Code Playgroud)

我在浏览器中得到的是:

Cache-Control:no-store
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
Run Code Online (Sandbox Code Playgroud)

我想要的是:

Cache-Control:no-cache, no-store, must-revalidate
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
Run Code Online (Sandbox Code Playgroud)

java cache-control spring-boot

23
推荐指数
4
解决办法
3万
查看次数

如何使用Spring MVC和Spring Security为资源处理程序启用HTTP缓存

我希望为一些静态资源启用HTTP缓存,例如图像,其访问受到Spring Security的限制.(这些资源不是安全关键,但也不应公开访问).如何避免让Spring Security添加禁用缓存的HTTP响应头?

如果我添加setCachePeriod()到我的资源处理程序注册WebMvcConfigurerAdapter.addResourceHandlers()如下:

registry.addResourceHandler("/static/**")
  .addResourceLocations("classpath:/static/").setCachePeriod(3600);
Run Code Online (Sandbox Code Playgroud)

仍然返回资源以及禁用缓存的以下标头:

Cache-Control: max-age=3600, must-revalidate
Expires: Mon, 04 Aug 2014 07:45:36 GMT
Pragma: no-cache
Run Code Online (Sandbox Code Playgroud)

我想避免在项目中引入任何XML配置,该项目目前仅使用Java注释配置.

有没有比扩展Spring资源处理程序更好的解决方案?

java spring caching spring-mvc spring-security

11
推荐指数
1
解决办法
1万
查看次数

Spring Security - 无法避免缓存控制

我有一个应用程序,并使用spring的控制器映射将图像加载到我的用户.(InputStream,响应等).

在我的控制器中,我将标头设置为缓存控制,基于文件等等.但是总是存在pragma:no-cache和Cache-Control:所有请求中的"max-age = 0",这取代了我的响应设置.

我正在努力解决这个问题,但没有任何作用.

我已经阅读了所有页面并尝试了我发现的所有内容:http: //docs.spring.io/autorepo/docs/spring-security/3.2.0.CI-SNAPSHOT/reference/html/headers.html

我的spring security.xml有:

    <security:headers disabled="true"/>
Run Code Online (Sandbox Code Playgroud)

任何人都有一个好主意来解决这个问题?

请记住,要加载我需要通过控制器加载的图像,我从不直接调用静态.

spring caching spring-security

7
推荐指数
3
解决办法
2万
查看次数

如何有选择地禁用 Spring Boot 的缓存(manifest.appcache)

从这个问题可以看出,Spring Security 管理 Spring Boot 的缓存。从 Spring Boot文档中,它展示了如何使用以下命令设置资源缓存:

spring.resources.cache-period= # cache timeouts in headers sent to browser
Run Code Online (Sandbox Code Playgroud)

cache-period对于 Spring Boot 的所有预定义静态位置(即/css**/js/**/images/**)来说非常有用,但我还生成了一个manifest.appcache用于离线下载我的静态资产的文件,并且由于上述所有 Spring Security/Boot 都会使用 manifest.appcache 发送回缓存标头

"method": "GET",
"path": "/manifest.appcache",
"response": {
    "X-Application-Context": "application:local,flyway,oracle,kerberos:8080",
    "Expires": "Tue, 06 Oct 2015 16:59:39 GMT",
    "Cache-Control": "max-age=31556926, must-revalidate",
    "status": "304"
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何添加排除项manifest.appcache。无论我的标头如何,IE 和 Chrome 似乎都会对 appcache 进行“正确的操作”,但 FF 似乎在注意到 appcache 发生更改时更加特殊,并且我认为我的缓存标头将其搞砸了。

编辑:我应该从WebMvcAutoConfiguration的源代码中添加它,它显示了如何为资源设置缓存,我只是不确定如何有选择地禁用我的 1 个案例,而不会潜在地破坏 spring boot 在该文件中设置的其余内容。 …

html5-appcache spring-boot

5
推荐指数
1
解决办法
8983
查看次数

如何在spring boot中启用浏览器缓存

我正试图让Spring启动让浏览器缓存静态资源.我的资源位于"静态"下的类路径中.当我查看发回的标头时,我看到修改标头设置正常,但不知何故,标题"Cache-Control:no-store"也被添加.

HTTP/1.1 200
Last-Modified: Wed, 24 Aug 2016 08:50:16 GMT
Cache-Control: no-store
Accept-Ranges: bytes
Content-Type: text/css
Content-Length: 434554
Date: Wed, 24 Aug 2016 09:42:42 GMT
Run Code Online (Sandbox Code Playgroud)

我已经看到了这个答案如何在Spring Boot中启用HTTP响应缓存,但这似乎并不适用于我,因为我没有使用spring-security,它不在类路径上.

我正在使用带有百里香的sp​​ring-boot 1.4.0.

那么,如何让spring boot不包含Cache-Control头?

java spring caching spring-mvc spring-boot

5
推荐指数
2
解决办法
3989
查看次数