【发布时间】: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