SpringSecurity认证和权限

导入SpringSecurity依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

然后继承

WebSecurityConfigurerAdapter

重写configure方法
 http.authorizeRequests().antMatchers("index").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
http.formLogin();

设置密码编码格式 扮演的角色 授权
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("tang").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
.and()
.withUser("gaoyu").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3");


}
上一篇:[剑指OFFER] 数组中的逆序对


下一篇:SpringBoot整合SpringSecurity完整实例