package com.gmy.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//aop思想:拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
//链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只有有权限的人才能访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll()//所有人都可以访问
.antMatchers("/level1/**").hasRole("vip1")//vip1的可以访问level1
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限会到登陆页面,需要开启登录页面,定制登录页
http.formLogin().loginPage("/toLogin");
//注销功能
http.csrf().disable();//关闭csrf,
http.logout().logoutSuccessUrl("/");
//开启记录我功能 cookie功能 ,默认两周
http.rememberMe();
}
//认证
//There is no PasswordEncoder mapped for the id "null"
//在spring secutiry 增加了更多加密方式
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常从数据库中取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("gmy").password(new BCryptPasswordEncoder().encode("135246")).roles("vip1","vip2","vip3")//设置一个用户的权限级别
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("135246")).roles("vip1","vip2")
.and().
withUser("ozl").password(new BCryptPasswordEncoder().encode("135246")).roles("vip3");
}
}