1,hasAuthority方法
自定义403页面
在配置里面进行配置
@Override
protected void configure(HttpSecurity http) throws Exception{
//配置没有权限访问跳转自定义页面
http.exceptionHandling().accessDeniedPage("/unauth.html");
http.formLogin()//自定义登录页面
.loginPage("/login.html") //登录页面设置
.loginProcessingUrl("/user/login") //登陆访问路径
.defaultSuccessUrl("/test/index").permitAll() //登陆成功之后,跳转路径
.and().authorizeRequests()
.antMatchers("/","/test/hello","/user/login").permitAll() //设置那些路径可以直接访问,不需要认证
// .antMatchers().hasAuthority("admins")
// .antMatchers().hasAnyAuthority("admins,manager")
.antMatchers("/test/index").hasRole("role")
// .antMatchers().hasAnyRole("role,sale")
.anyRequest().authenticated()
.and().csrf().disable(); //关闭crsf防护
}
2,hasAnyAuthority方法
3,hasAnyRole方法
特别注意!!!
在hasAnyRole方法源码中拼接了一个字符串前缀ROLE_,当我们在设置用户权限的时候,需要自行添加前缀ROLE_,
4,hasAnyRole方法