Spring 安全性导致 404 并显示消息“无静态资源登录”

问题描述 投票:0回答:1

设法生成一个简单的示例。尝试了很多谷歌搜索,但没有一个答案有帮助。

简单的测试代码:

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());
    }
}

测试控制器:

@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";
    }
}

使用 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();
    }
}

运行测试并得到结果:

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

如果该 bean 在配置测试中被注释掉,则测试通过。

Windows 10、JDK 21、Springboot 3.2.3、Junit Jupiter

困惑了 2 天,非常感谢任何帮助。

spring-boot spring-mvc spring-security
1个回答
0
投票

我也有类似的问题,同样的错误。我还没有完全弄清楚,但它与@ContextConfiguration有某种关系。我将其更改为@Import,它有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.