簡體   English   中英

從spring-starter-security中排除網址

[英]Exclude urls from spring-starter-security

我在使用springboot-starter-security時遇到問題。 我只想保護不以“ / api”開頭的網址,所以必須不保護所有網址,例如“ / api”或“ / api /”或“ / api / **”。

在WebSecurityConfigClass中,我有:

@Override
    public void configure(WebSecurity web) throws Exception {

        web.ignoring().antMatchers("/api*");

    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");
    }

在應用程序中,我有兩個不同的控制器,即webcontroller和restcontroller。 目前還沒有實現Web,但是它必須解決我想要保護的URL,其余的控制器管理不需要驗證的URL。 我有一個用於rest控制器的測試類,所有測試均失敗,因為它們期望狀態為200,但它們卻收到401。例如:

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

此測試由於狀態為401而不是200而失敗。為什么?

我解決了,測試類沒有使用websecurityconfig類。 我通過添加顯式導入的WebSecurityConfig作為類批注來進行修復:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers=ARestController.class)
@Import(WebSecurityConfig.class) // <---
public class ARestControllerTest { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM