簡體   English   中英

我可以只允許用戶使用某些路由而無需使用 spring 引導登錄嗎?

[英]Can i allow a user only to certain routes without logging in with spring boot?

晚上好。 我現在想知道如何在我的應用程序中配置 spring 引導,以允許用戶只能訪問我的 web 站點的索引頁面和 swagger 文檔的頁面。 任何其他路由都應重定向到默認的 spring 啟動登錄頁面:我試過這個配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
    
       http.authorizeRequests().antMatchers(
                        "/",
                        "/swagger-ui.html"
        ).permitAll();
       http.authorizeRequests().antMatchers("/*").permitAll().anyRequest().authenticated().and().formLogin();
     

    }

和這個:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(authz -> authz.antMatchers("/*", "swagger-ui.html").permitAll().anyRequest().authenticated()).formLogin();

    }

但他們都沒有為我的目的而工作。 謝謝你的回答

我用這個配置解決了

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/*","swagger-ui.html").permitAll();

http.authorizeRequests().antMatchers("/web/**").authenticated().and()
.formLogin();

}

現在我沒有經過身份驗證的訪問權限,只能訪問索引頁面和 swagger 頁面。 其他任何東西都需要身份驗證,這正是我想要的。

暫無
暫無

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

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