Spring Security的声明式安全授权有两种方式,一种是以url模式匹配的方式,另一种是方法上使用注解声明权限,这里重点说第一种。
一、创建一个类继承WebSecurityConfigurerAdapter,并使用注解@EnableWebSecurity标注。这个类我之前写到过很多次,是配置CAS客户端和Spring Security的核心类。同时也是启动注解声明权限的入口。
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationManager authenticationManager;
// 这个方法名是随便起的
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//暂时使用基于内存的AuthenticationProvider,在弹出的默认登陆页输入下边的用户名和密码
//auth.inMemoryAuthentication().withUser("user").password("1").roles("USER");
}
/**
* 这里是默认的配置相当于以下命名空间的配置
* <http>
* <intercept-url pattern="/**" access="authenticated"/>
* <form-login />
* <http-basic />
* </http>
*/
protected void configure(HttpSecurity http)