Spring Security重定向到状态码为999的页面
成功登录后,spring重定向到/error包含以下内容的页面
{  "timestamp" : 1586002411175,
  "status" : 999,
  "error" : "None",
  "message" : "No message available"
}
我正在使用 spring-boot 2.2.4
回答:
spring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.jsp
spring.mvc.servlet.load-on-startup=1
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
@Configurationpublic class DispatcherContextConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}
@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");
    }
    // ...
}
回答:
原来,该错误是由于对我的一个静态资源的请求失败而引起的。登录页面<script src="/resources/my-js-
file.js"></script>缺少该项目。我可以通过删除丢失的资源导入来解决此问题,但是该问题将来可能会再次出现,因此不是解决方案。
回答:
我知道我可以强制重定向到起始页面,.defaultSuccessUrl("/log",
true)但我不希望这样做。我也想重定向,即使没有找到任何资源也能正常工作。
回答:
浪费了很多时间后,我才知道发生了什么事。
因此,spring无法找到登录页面上使用的静态资源之一。但是,404它尝试呈现错误页面并将请求转发给,而不是返回此资源的状态/error。然后Spring
Security拒绝此请求,因为用户未被授权。它将/error请求保存到会话中(用于成功登录后进行重定向),并将用户重定向到登录页面。
当然,用户看不到此重定向,因为状态302在后台完成的请求返回。但是主要问题是/error请求保存在会话中。
然后用户成功登录,并spring检查该属性的会话并重定向到/error页面。默认情况下,spring假设您在静态资源中的某处有这样的页面。如果没有该页面,您将看到状态码为999的此奇怪错误。
回答:
忽略/error安全配置中的页面:
web.ignoring().antMatchers("/favicon.ico", "/resources/**", "/error");因此,成功登录后,此请求将不会保存到会话中以供用户重定向。您会在登录页面上看到对静态资源的请求的状态码将从302变为404。
回答:
忽略spring boot autoconfig的一部分:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration这给出了相同的结果,但是从配置中禁用了一些bean,ErrorMvcAutoConfiguration所以要小心。
以上是 Spring Security重定向到状态码为999的页面 的全部内容, 来源链接: utcz.com/qa/434884.html

