问题
I have a typical security configuration:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/index").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
//http.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
}
Now spring is asking for a login for everything except of the login page... But how could I make it the other way, so it asks for login page ONLY for the view /index? Od only for a group of endpoints? Thanks!
回答1:
Try this:
http
.authorizeRequests()
.antMatchers("/", "/index").authenticated()//Makes / and /index to be authenthicated
.anyRequest().permitAll()//Makes any other allow without authentication. Or if you want a group use antMatchers here too.
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
来源:https://stackoverflow.com/questions/61273204/apply-spring-security-only-to-one-page