其他分享
首页 > 其他分享> > SpringSecurity认证和权限

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");


}

标签:SpringSecurity,vip3,vip2,BCryptPasswordEncoder,antMatchers,认证,hasRole,new,权限
来源: https://www.cnblogs.com/tangxuyang/p/14868308.html