【问题标题】:Make JSF resources publicly accessible with Spring Security使用 Spring Security 使 JSF 资源可公开访问
【发布时间】:2016-08-22 19:03:30
【问题描述】:

我已经在我的 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效果。

【问题讨论】:

    标签: java spring-mvc jsf spring-security


    【解决方案1】:

    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/** 的请求匹配的每个响应添加一个标头编写器:

    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();
    

    另请参阅:

    【讨论】:

    • 我添加了“/javax.faces.resource/**”。不幸的是,这使得整个站点授权给未登录的用户。每个人都可以在不登录的情况下访问所有内容。
    • 是的!我确实尝试过“/javax.faces.resource/**”几次,但我每次都错过了这个 .antMatchers("/**").authenticated()。所以我的网站总是公开的
    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 2011-05-23
    • 2013-08-20
    • 2015-10-14
    • 2015-09-18
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多