6.Springboot集成安全框架

SpringSecurity

1.Springboot集成SpringSecurity

它是一个安全组件:shiro

在web开发中,安全是第一位的,我们可以通过拦截器、过滤器来做权限认证

安全应该在什么时候考虑?在开始架构和设计的时候就应该考虑了

漏洞、隐私

撞库:一般要求存入到数据库的密码都是MD5,所有用户名和密码 icodingedu.com guanyu/guan1201

我们进行代码开发一路走来

SpringSecurity:其实他是一个完整的AOP思想体现,⾯面向切⾯面,不需要侵入任何业务代码就能实现安全验证

1.1SpringSecurity简介

SpringSecurity是针对Spring项目的安全框架,也是Springboot底层安全模块默认的技术选型,只需要引入spring-boot-starter-security模块,进行少量的配置就可以进行强大的安全管理理,涉及以下几个核心的类

WebSecurityConfigurerAdpater:

自定义Security策略 //授权:

@EnableWebSecurity
public class SSConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页放开所有人访问,功能页让有权限的用户访问,SS是链式编程
        //请求授权的规则
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //失败是跳入403页面,我们开启一下登录验证
        //跳的是SS自己的登录页面login,我们需要下载源码来看一下注释,这里注释的内容非常多
        http.formLogin()
                .loginPage("/toLogin")
                .loginProcessingUrl("/login").usernameParameter("uname").passwordParameter("pwd")
                .defaultSuccessUrl("/",true)
                .and().exceptionHandling().accessDeniedPage("/toError");
        //注销登录
        http.logout()
                .deleteCookies("remove").invalidateHttpSession(true)
                .logoutSuccessUrl("/");
        //CSRF网站攻击,get方式如果出现404需要关闭csrf跨站攻击
        http.csrf().disable();
        //开启记住我功能
        http.rememberMe().rememberMeParameter("remember");
    }
    //认证 这种在内存里调用在springboot 2.1.x可以,但2.2.1是有问题的
    //设置密码编码passwordencoder
    //在spring security 5+ 加入了很多加密方式,不加密会认为非常不安全,反编译后就是明文
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("gavin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2")
//                .and()
//                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3")
//                .and()
//                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
//    }

}

AuthenticationManagerBuilder:

自定义认证策略 //认证

@EanbleWebSecurity:开启Security模式

SpringSecurity的两个主要目标是“认证”和“授权”(其实就是访问控制)

1.2开始导入SpringSecurity

  <dependency>            
			<groupId>org.springframework.boot</groupId>           

            <artifactId>spring-boot-starter-security</artifactId>       
  </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值