简体   繁体   中英

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

Good evening. I would like to now how it could be possible to configure spring boot in my application to allow users to have access only to the index page of my web site and the page of the swagger documentation. any other route should redirected to the default spring boot login page: i tried this configuration:

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

    }

and this:

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

    }

but none of them are working for my purpose. thank you for answering

i solved it with this configuration

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

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

}

now i have no authenticated access only to the index page and the swagger page. anything else needs authentication which is exactly what i wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM