【问题标题】:Spring security redirect to static resources after authentication认证后Spring安全重定向到静态资源
【发布时间】:2020-10-13 08:20:36
【问题描述】:

早上好, 我正在使用 Spring security(最新版本)和 Spring MVC(最新版本)编写应用程序。

确保一切正常后,表单和控制器以及Spring安全配置都运行良好,登录始终成功。

然后我添加了一个自定义 css 和一些图像以显示更好的图形,问题就来了: 每次登录时,我都会首先重定向到 login.css 文件而不是主页,虽然登录成功,但我首先会看到一个空白页面,其中包含 css 的 url。

Spring 安全配置

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests()
            //configure security for pages
            .antMatchers(new String[]{"/login", "/accessDenied"}).permitAll()
            .antMatchers("/**").access("hasAnyRole('admin', 'operatore')")
            .anyRequest().authenticated()
            //creates login form
            .and().formLogin().loginPage("/login").loginProcessingUrl("/login")
            .defaultSuccessUrl("/home").failureUrl("/accessDenied")
            .usernameParameter("id_utente").passwordParameter("password")
            //catches exceptions http 403 response
            .and().exceptionHandling().accessDeniedPage("/accessDenied");
    
    http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}

静态资源位于路径/static/css/static/images 下。 我读到这个问题来自这样一个事实,即 Spring 安全将您重定向到登录后最后请求的 url,以解决我尝试使用的问题

@Override
public void configure(WebSecurity web)
{
    web.ignoring().antMatchers("/static/**");
}

但是没有用。

有没有办法解决这个问题?

编辑

我已经尝试过使用

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests()
            //configure security for pages
            .antMatchers("/static/**").permitAll()
            .antMatchers(new String[]{"/login", "/accessDenied"}).permitAll()
            .antMatchers("/**").access("hasAnyRole('admin', 'operatore')")
            .anyRequest().authenticated()
            //creates login form
            .and().formLogin().loginPage("/login").loginProcessingUrl("/login")
            .defaultSuccessUrl("/home").failureUrl("/accessDenied")
            .usernameParameter("id_utente").passwordParameter("password")
            //catches exceptions http 403 response
            .and().exceptionHandling().accessDeniedPage("/accessDenied");
    
    http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}

但无论如何我在成功登录后重定向到 404 页面,网址为“http://localhost:8080/static/css/login.css”。在登录页面中,静态资源被正确地提供为我所期望的样式。

编辑#2

我像 Eleftheria Stein-Kousathana 所说的那样进行了编辑,但它一直重定向到 css(这次显示代码)而不是主页。 项目结构为:

资源处理程序以这种方式添加:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
    registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}

更新的配置是:

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests()
            //configure security for pages
            .antMatchers("/css/**", "/images/**").permitAll()
            .antMatchers(new String[]{"/login", "/accessDenied"}).permitAll()
            .antMatchers("/**").access("hasAnyRole('admin', 'operatore')")
            .anyRequest().authenticated()
            //creates login form
            .and().formLogin().loginPage("/login").loginProcessingUrl("/login")
            .defaultSuccessUrl("/home").failureUrl("/accessDenied")
            .usernameParameter("id_utente").passwordParameter("password")
            //catches exceptions http 403 response
            .and().exceptionHandling().accessDeniedPage("/accessDenied");
    
    http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}

在登录页面内,css 是这样链接的:

        <link rel="stylesheet" href="<c:url value="/static/css/login.css" />">

使用新的antMatchers,结果不是 404,而是 css 中的代码。顺便说一句,如果我使用新配置和“/static/css/login.css”或“/css/login.css”,则不再提供链接的css,因为样式无法正确显示,这两个链接都不起作用.

【问题讨论】:

    标签: spring jsp spring-security graphics


    【解决方案1】:

    由于static/cssstatic/imagesstatic目录下,它们将分别在/css/images提供服务。

    您可以在 HTTP 安全配置中允许对这些资源的所有请求。

    http.authorizeRequests()
        .antMatchers("/css/**", "/images/**").permitAll()
        // ...
    

    【讨论】:

    • 我已经尝试以.antMatchers("/static/**").permitAll() 的形式使用它,资源被正确地提供为我所期望的样式,但是登录后我再次被重定向到一个 404 页面url "localhost:8080/static/css/login.css" 而不是.defaultSuccessUrl("/home")中所述的主页
    • localhost:8080/static/css/login.css 未找到,因为资源应在 localhost:8080/css/login.css 上提供。在您的 HTML 页面中,确保您引用的是 /css/login.css 而不是 /static/css/login.css。那么授权规则应该起作用了。
    • 或者,如果你总是想在登录后重定向到同一个页面,你可以在configure方法http.requestCache().disable()中禁用请求缓存
    • 我编辑了问题以显示更改后的新情况。
    • 对不起,我最近看到第二条关于禁用请求缓存的评论,它实际上在我被重定向到主页时起作用,但它对性能有任何其他影响还是只影响它特定的重定向?
    猜你喜欢
    • 2017-03-29
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2020-07-14
    • 2014-10-30
    • 2020-06-19
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多