Spring security causing 404 with message "No static resource login"

43 views Asked by At

Managed to produce a simple example. Tried lots of Googling but none of the answers helped.

simple test code:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(TestController.class)
@ContextConfiguration(classes={SimpleTestConfig.class})
public class Producing404ExampleTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
        mvc.perform(get("/login"))
            .andExpect(status().isOk());
    }
}

The test controller:

@Controller
public class TestController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model ) {
        model.addAttribute("app_name", "testing");
        model.addAttribute("page_description", "login page");
        return "login";
    }
}

Configured with SimpleTestConfig:

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@SpringBootConfiguration
public class SimpleTestConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        HTTP
            .authorizeHttpRequests(authorize -> authorize
                .requestMatchers("/static**", "/logout/**", "/login/**", "/health/**").permitAll()
                .requestMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().permitAll()
        );

        return http.build();
    }
}

Run the test and get the result:

MockHttpServletRequest:
  HTTP Method = GET
  Request URI = /login
   Parameters = {}
      Headers = []
         Body = null
Session Attrs = {}

Handler:
         Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
         Type = org.springframework.web.servlet.resource.NoResourceFoundException

ModelAndView:
    View name = null
         View = null
        Model = null

FlashMap:
   Attributes = null

MockHttpServletResponse:
       Status = 404
Error message = No static resource login.
      Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
 Content type = null
         Body = 
Forwarded URL = null
Redirected URL = null
      Cookies = []

Status
Expected :200
Actual   :404

If the bean is commented out in the config test passes.

Windows 10, JDK 21, Springboot 3.2.3, Junit Jupiter

Been confused for 2 days now any assistance is much appreciated.

0

There are 0 answers