常用:SpringSecurity
身份验证+权限控制 框架
SpringSecurity授权认证代码模版
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/page1/**").hasRole("A")
.antMatchers("/page2/**").hasRole("B")
.antMatchers("/page3/**").hasRole("C");
// 无权限会回到登录页面
http.formLogin().loginPage("/login");
// 开启注销功能
http.csrf().disable();
http.logout().logoutSuccessUrl("/");
// 开启记住我功能
http.rememberMe().rememberMeParameter("remember");
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这里拓展需要连数据库
//密码需要加密,不然会失败
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("Qiddo").password(new BCryptPasswordEncoder().encode("123456")).roles("A", "B")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("A", "B", "C")
.and()
.withUser("man").password(new BCryptPasswordEncoder().encode("123456")).roles("A");
}
}
注:代码中认证部分没有用到数据库去判别身份,直接自己在后端创建的,具体的复杂代码还得看官方手册注解去实现更多的认证功能,授权同理,很多都是自定义的。

被折叠的 条评论
为什么被折叠?



