java - 使用 Spring Security 使 JSF 资源可公开访问

标签 java spring-mvc jsf spring-security

我已经在我的 jsf 应用程序中实现了 spring security。除了静态资源需要身份验证之外,一切工作正常。这是我的配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.authorizeRequests()
            .antMatchers("/register", "/resources/**").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().loginPage("/login").permitAll()
           .usernameParameter("username").passwordParameter("password")
            .and().exceptionHandling().accessDeniedPage("/Access_Denied");
}

经过一番谷歌搜索,大多数解决方案是添加 mvc 资源标签。

  <mvc:resources mapping="/resources/**" location="/resources/"
    cache-period="31556926"/>

我找到了类似的注释并为此添加了一个配置类

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    // equivalents for <mvc:resources/> tags
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
    }

    // equivalent for <mvc:default-servlet-handler/> tag
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

但是静态资源仍然需要身份验证。关于如何完成这项工作,一些帮助会很好。

注意:我的资源放置在 /src/main/webapp/resources/{css|js|image} 中。问题是如果用户没有登录,css、js的效果不会在登录页面显示。用户登录一次后,登录后来到登录页面,出现css效果。

最佳答案

JSF 托管库资源由 /javax.faces.resource/** 路径提供。因此,您需要使该路径可公开访问:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.authorizeRequests()
        .antMatchers("/register", "/javax.faces.resource/**").permitAll()
        .antMatchers("/**").authenticated()
        .and().formLogin().loginPage("/login").permitAll()
        .usernameParameter("username").passwordParameter("password")
        .and().exceptionHandling().accessDeniedPage("/Access_Denied");
}

您可能还希望浏览器缓存这些资源。然后,将此部分添加到您的配置中,这将为每个与 /javax.faces.resource/** 请求匹配的响应添加一个 header 编写器:

http.headers()
        .addHeaderWriter(new DelegatingRequestMatcherHeaderWriter(
                new AntPathRequestMatcher("/javax.faces.resource/**"),
                new HeaderWriter() {

                    @Override
                    public void writeHeaders(HttpServletRequest request,
                            HttpServletResponse response) {
                        response.addHeader("Cache-Control", "private, max-age=86400");
                    }
                }))
        .defaultsDisabled();

另请参阅:

关于java - 使用 Spring Security 使 JSF 资源可公开访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39086947/

相关文章:

java - 将 int[] 转换为 char[] 无法正常工作

spring-mvc - 我应该如何将自定义 SiteMeshFilter 与 Spring 的 AbstractAnnotationConfigDispatcherServletInitializer 的实现一起使用?

javascript - 使用 spring mvc 和 apache pdf 框从 UI 下载的空的和损坏的 pdf 文件

java - 我工作场所的代理阻止我使用 Maven

java - 从 jSF 中的 ActionListener 重定向

jsf 消息严重性

java - 为什么我的sql代码在结果集中无法正常运行

java - 使用 Spark-avro 写入数据帧创建 topLevelRecord - 想要使用现有架构

java - OOP 和私有(private)字段的继承

java - 当 a4j :commandLink and a4j:commandButton not working properly 时,Richfaces 4 和 JSF2.0 出现问题