上一篇文章《Spring Security(二)–Guides》,通过Spring Security的配置项了解了Spring Security是如何保护我们的应用的,本篇文章对上一次的配置做一个讲解。
这是Spring Security入门指南中的配置项:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("USER");
}
}
当配置了上述的javaconfig之后,我们的应用便具备了如下的功能:
我们自己定义的配置类WebSecurityConfig加上了@EnableWebSecurity注解,同时继承了WebSecurityConfigurerAdapter。你可能会在想谁的作用大一点,先给出结论:毫无疑问@EnableWebSecurity起到决定性的配置作用,他其实是个组合注解,背后SpringBoot做了非常多的配置。