快速上手一个SpringSecurity

如何快速上手一个SpringSecurity?
简介:Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
一、首先需要导入SpringSecurity依赖:

<!--Security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
         <!--Thymeleaf- security-->
         <!---Security整合Thymeleaf时要导入的包-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

二、我们在一个Config包中写上SecurityConfig类:
2.1.写好Security类以后,我们需要在类上加一个
@EnableWebSecurity注解,我们是基于AOP来写的。
2.2我们需要在继承一个WebSecurityConfigurerAdapter类,
接着我们重写两个方法configure。有两个configure方法:第一个http参数的是授权。
第二个是认证方法。

protected void configure(HttpSecurity http) throws Exception {
}
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
 }

三、授权中有很多权限如例如如下代码中:

//AOP:拦截器
@EnableWebSecurity
public class SecurityConfig extends 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");
        //没有权限默认会到Login页面,需要开启到登录的页面
        http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");
        //注销
        //防止网站攻击 ,get post
        http.csrf().disable();//关闭csrf  登录失败可能存在的原因
        http.logout().logoutSuccessUrl("/");
        http.rememberMe().rememberMeParameter("remember");
    }
    //认证 ,springboot2.1.x 可以直接使用
    //密码编码:PasswordEncoder
    //在spring security5.0新增加了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //正常的话这些数据都应该从数据库中读
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("zhaoguoshun").password(new BCryptPasswordEncoder().encode("985211")).roles("vip2")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("985211")).roles("vip1","vip2","vip3")
                .and()
                .withUser("yige").password(new BCryptPasswordEncoder().encode("985211")).roles("vip1");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值