gpt4 book ai didi

java - 使用 spring security 处理静态资源

转载 作者:行者123 更新时间:2023-11-30 09:28:12 27 4
gpt4 key购买 nike

你好,我需要使用 Spring Security 处理的静态资源,我希望仍然保持静态,意思是它没有使用 DispatcherServlet 处理.我为非安全资源保留了一个文件夹,为安全资源保留了一个文件夹。在排除 /res/secured 之前,我无法完成这项工作来自资源处理程序。但是,如果我这样做,安全资源将使用 DispatcherServlet 进行处理。我认为这是不对的(也许我错了?-> 发表解释或链接)。

我的配置:

/*--- Directories structure ---*/
res
|-- nonsecured
|-- secured
/*--- /Directories structure ---*/

/*--- WebApplicationInitializer ---*/
Dynamic portalSecurityFilter = servletContext.addFilter("portalSecurityFilter", new PortalSecurityFilter());
portalSecurityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

// Spring Security filtr
Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");

Dynamic dynamicCharacterEncodingFilter = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
dynamicCharacterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

Dynamic ajaxFilter = servletContext.addFilter("ajaxFilter", new AjaxFilter());
ajaxFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

// Root context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebConfig.class);

// Dispatcher servlet
ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(rootContext));
dispatcherServlet.setLoadOnStartup(1);
dispatcherServlet.addMapping("/");

servletContext.addListener(new ContextLoaderListener(rootContext));
/*--- /WebApplicationInitializer ---*/

/*--- Web configuration part ---*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/res/**").addResourceLocations("/WEB-INF/res/");
}
/*--- /Web configuration part ---*/

/*--- Spring Security confogiration part ---*/
<http pattern="/res/unsecured/**" security="none" />

<http pattern="/**" use-expressions="true" authentication-manager-ref="myAuthenticationManager">

<intercept-url pattern="/res/secured/**" access="hasRole('ROLE_USER_AUTHENTICATED')" />
<intercept-url pattern="/**" access="permitAll" />
</http>
/*--- /Spring Security confogiration part ---*/

感谢您的回答。

编辑

As I playing around it seems to me that <http
pattern="/res/unsecured/**" security="none" />
part in security configuration is pointless because resources served by resource handler don't go through Spring Security filter chain. Am I missing something or is my configuration wrong?

最佳答案

我必须承认这是我的耻辱。 Spring Security 按预期工作,上面的配置运行良好。我的问题是浏览器缓存了静态资源(即 PDF 文件),我只是没有注意到它。如果您遇到同样的问题,请尝试进行硬刷新,然后再花时间搜索问题:)

关于java - 使用 spring security 处理静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159824/

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