首页 > 其他分享 >判定登录权限

判定登录权限

时间:2022-10-12 11:24:19浏览次数:42  
标签:withUser 权限 123456 登录 roles antMatchers auth 判定 password

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问

//请求授权规则
http.authorizeHttpRequests()
.antMatchers("/").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("weihuan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password("123456").roles("vip1","vip2","vip3")
.and()
.withUser("guest").password("123456").roles("vip1");
}


}
---------------------------------------------------------------------------------------------------------------------------------------------------
//数据库读取
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// 确保密码正确编码
UserBuilder users = User.withDefaultPasswordEncoder();
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser(users.username("user").password("password").roles("USER"))
.withUser(users.username("admin").password("password").roles("USER","ADMIN"));
}

标签:withUser,权限,123456,登录,roles,antMatchers,auth,判定,password
From: https://www.cnblogs.com/wh521/p/16783855.html

相关文章