2021-01-19

SpringBoot 整合Srcurity 简单测试

开启Security

@EnableWebSecurity

SecurityConfig继承WebSecurityConfigurerAdapter

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权
//  首页所有人可以访问
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有登录或者没有权限的时候,自动跳转登录界面.loginPage("/toLogin")
        http.csrf().disable().formLogin().loginPage("/toLogin");
//       开启自动配置 开启注销 注销完成后进入登录界面
        http.logout();
    }

//    @Bean
//    PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
//        定义在内存中,密码一定要加密
        auth.inMemoryAuthentication().passwordEncoder(encoder)
                .withUser("peterW").password(encoder.encode("123456")).roles("vip2","vip3").and()
                .withUser("root").password(encoder.encode("123456")).roles("vip1","vip2","vip3").and()
                .withUser("guest").password(encoder.encode("123456")).roles("vip1");
    }
}

项目发布在码云上的,注意实现的是不通权限登入有不同的模块显示。在security中
https://gitee.com/wang-wenlin/springboot_security_shiro/tree/master

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值