SpringBoot 整合Srcurity 简单测试
开启Security
@EnableWebSecurity
SecurityConfig继承WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception {
//授权
// 首页所有人可以访问
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有登录或者没有权限的时候,自动跳转登录界面.loginPage("/toLogin")
http.csrf().disable().formLogin().loginPage("/toLogin");
// 开启自动配置 开启注销 注销完成后进入登录界面
http.logout();
}
// @Bean
// PasswordEncoder passwordEncoder(){
// return new BCryptPasswordEncoder();
// }
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = new BCryptPasswordEncoder();
// 定义在内存中,密码一定要加密
auth.inMemoryAuthentication().passwordEncoder(encoder)
.withUser("peterW").password(encoder.encode("123456")).roles("vip2","vip3").and()
.withUser("root").password(encoder.encode("123456")).roles("vip1","vip2","vip3").and()
.withUser("guest").password(encoder.encode("123456")).roles("vip1");
}
}
项目发布在码云上的,注意实现的是不通权限登入有不同的模块显示。在security中
https://gitee.com/wang-wenlin/springboot_security_shiro/tree/master