Java Spring security重定向到状态代码为999的页面
成功登录后,spring将重定向到包含以下内容的Java Spring security重定向到状态代码为999的页面,java,spring-security,Java,Spring Security,成功登录后,spring将重定向到包含以下内容的/error页面 { "timestamp" : 1586002411175, "status" : 999, "error" : "None", "message" : "No message available" } 我正在使用springboot2.2.4 我的配置: 注: 结果表明,该错误是由对我的一个静态资源的请求失败引起的。登录页面包含项目中缺少的。 我可以通过删除丢失的资源导入来修复此问题,但问题可能会在将来再次
/error
页面
{
"timestamp" : 1586002411175,
"status" : 999,
"error" : "None",
"message" : "No message available"
}
我正在使用springboot2.2.4
我的配置:
注: 结果表明,该错误是由对我的一个静态资源的请求失败引起的。登录页面包含项目中缺少的
。
我可以通过删除丢失的资源导入来修复此问题,但问题可能会在将来再次出现,因此它不是修复
我怎样才能防止这种情况发生?
我知道我可以使用.defaultSuccessUrl(“/log”,true)
强制重定向到起始页面,但我不想这样。
另外,我希望重定向能够正常工作,尽管没有找到任何资源。在浪费了很多时间之后,我知道发生了什么 所以spring找不到登录页面上使用的静态资源。但它不会返回此资源的状态
404
,而是尝试呈现错误页面并将请求转发到/error
。然后,spring security会拒绝此请求,因为用户未经授权。它将/error
请求保存到会话(用于在成功登录后重定向),并将用户重定向到登录页面
当然,用户无法看到此重定向,因为status302
返回在后台完成的请求。但主要的问题是会话中保存的/error
请求
然后用户成功登录,spring检查会话中的该属性,并重定向到/error
页面。默认情况下。如果您没有此页面,您将看到此奇怪的错误,状态代码为999
解决方案1
忽略安全配置中的/error
页面:
web.ignoring().antMatchers("/favicon.ico", "/resources/**", "/error");
因此,在成功登录后,此请求将不会保存到会话以用于用户重定向。您将看到,在登录页面上,请求静态资源的状态代码将从302
更改为404
解决方案2
忽略spring启动自动配置的一部分:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
这会给出相同的结果,但会从configErrorMvcAutoConfiguration
中禁用一些bean,所以要小心
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/favicon.ico", "/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").permitAll()
.antMatchers("/registration/**").anonymous()
.anyRequest().authenticated()
.and()
.headers()
.defaultsDisabled()
.cacheControl()
.and()
.and()
.exceptionHandling()
.accessDeniedPage("/errors/403")
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/log") // I don't want to use force redirect here
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.rememberMe()
.rememberMeParameter("remember-me")
.key("myKey");
}
// ...
}
web.ignoring().antMatchers("/favicon.ico", "/resources/**", "/error");
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration