Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

510
Vistas
Spring Security 404 page for unauthenticated users

I'm using Spring Boot and Thymeleaf. I have a custom 404 template page defined in src/main/resources/templates/error/404.html

This works properly when users are logged in.

However, when they are logged out, they do not get any type of 404 page, they just get redirected back to /login.

I'm thinking my security configuration needs to change but not sure what.

@Override
protected void configure(HttpSecurity http) throws Exception {

  http.authorizeRequests()
      .antMatchers("/","/register*","/resetPassword","/forgotPassword","/login","/404").permitAll()
      .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
      .authenticated().and().formLogin().loginPage("/login").failureUrl("/login?error")
      .defaultSuccessUrl("/dashboard").successHandler(successHandler)
      .usernameParameter("email").passwordParameter("password")
      .and().logout()
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login?logout").and()
      .exceptionHandling().accessDeniedPage("/access-denied");
}

@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring().antMatchers("/error**","/resources/**", "/static/**", "/css/**", "/js/**", "/img/**");
}
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

First of all I encourage you to use indentation when using java config to configure your security for your spring application. It helps with readability.

Note all top level methods on the first indentation (authRequest,formLogin,logout) all configure/update the HTTP object it self. All these elements are from the org.springframework.security.config.annotation.web.builders.HttpSecurity class.

The children of these classes further refine the HTTP security configuration.

http
.authorizeRequests()
  .antMatchers("/","/register*","/resetPassword","/forgotPassword","/login","/404")
  .permitAll()
  .antMatchers("/admin/**").hasAuthority("ADMIN")
  .anyRequest().authenticated() // <--------
  .and()
.formLogin()
  .loginPage("/login")
  .failureUrl("/login?error")
  .defaultSuccessUrl("/dashboard")
  .usernameParameter("email").passwordParameter("password")
  .and()
.logout()
  .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  .logoutSuccessUrl("/login?logout")
  .and()
.exceptionHandling()
  .accessDeniedPage("/access-denied");

Note .anyRequest().authenticated() is specifically stating that any request must be authenticated. So when you attempt to goto any missing url on your domain it will ask you to login rather than goto the 404 page.

So if you remove that statement it and then try an goto a missing url page it will redirect you to a 404 page.

about 4 years ago · Santiago Trujillo Denunciar

0

If you remove .anyRequest().Authenticated() then you can log in without authenticated.

Therefore, do not try to delete. For example, if you go to the address "http://localhost:8080/user", then you will be taken to the authorization page. And if you try to enter the page "http://localhost:8080/user/" then you will be taken to the user page. Please note that links differ only by the forward slash at the end. Of course if you remove ".anyRequest().Authenticated()" in this case you need to add more parameters to antMatchers like "/user" and "/user/"

Therefore, be careful and attentive.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda