Spring boot的security安全控制--(1)spring security入门

本文介绍了Spring Security的基础知识,包括认证和授权流程。通过示例展示了如何在Spring Boot中配置Security,创建用户并设置权限。同时,文章还涵盖了核心类的解释以及一个简单的Spring Boot Security应用的搭建过程。
摘要由CSDN通过智能技术生成

Spring Security是什么?

是一个可以定制的安全控制框架。主要包括两个操作

  • 认证:确认用户可以访问当前系统
  • 授权:确认用户在当前系统中能够执行某个操作,即用户的功能权限

spring boot中使用

springboot中使用spring security只需要创建一个自定义类继承WebSecurityConfigurerAdapter,用过重写
configure方法来配置。

configure有两个方法:

  • configure(AuthenticationManagerBuilder auth)
  • configure(HttpSecurity http)

configure(HttpSecurity http)可以通过HttpSecurity的authorizeRequest()方法来定义哪些URL需要被保护;通过formLogin()方法定义当需要用户登录时候跳转的界面
configureGlobal(AuthenticationManagerBuilder auth)用于用户的创建以及角色的分配

用户认证:

通过configure(AuthenticationManagerBuilder auth)进行认证:

 @Override
    public void configure(AuthenticationManagerBuilder auth)throws Exception {
   
        System.out.println("AppSecurityConfigurer configureGlobal() 调用。。。。");
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("cc").password("123456").roles("USER");
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("admin").roles("ADMIN", "DBA");
    }

上述代码创建了一个user角色的账号cc 密码123456
一个"ADMIN", “DBA”)角色的账号admin 密码admin 两个用户;

用户授权

通过configure(HttpSecurity http)完成授权
HttpSecurity的authorizeRequest()方法有多个子节点,

  • antMatchers 使用ant风格匹配
  • regexMatchers 使用正则表达式匹配
    在匹配了请求路径后,可以针对当前用户的信息对请求路径进行安全处理,以下为Spring security提供的安全处理方法:
Method Description
anyRequest() 匹配所有请求路径.
access(String) Spring EL表达式结果为true时可以访问.
anonymous() 匿名可访问.
denyAll() 用户不能访问.
fullyAuthenticated() 用户完全认证时可访问.
hasAnyAuthority(String…) 如果用户有参数,则其中任一权限可访问.
hasAnyRole(String…) 如果用户有参数,则其中任一角色可访问.
hasAuthority(String) 如果用户有参数,则其权限可访问.
hasIpAddress(String) 如果用户来自参数中的IP则可访问.
hasRole(String) 如果用户有参数中的角色可访问.
permitAll() 用户可任意访问.
rememberMe() 允许通过remember-me登陆的用户访问.
authenticated() 用户登录后可访问

示例代码:

http.authorizeRequests() //开始请求权限配置
                .antMatchers("/login", "/css/**", "/js/**", "/img/**").permitAll() //请求匹配"/login",等界面,所有用户都可以访问
                .antMatchers("/", "/home").hasRole("USER")//请求"/", "/home",只有User角色*
                .antMatchers("/admin/**").hasAnyRole("ADMIN", "DBA")//请求"/admin/*",只有ADMIN或DBA角色才可以访问**
                .anyRequest().authenticated() //其余所有路劲都需要认证(登录)后才可访问
                .and()//其他配置
                .formLogin()//开始设置登录操作
                .loginPage("/login") //登录界面
                .successHandler(appAuthenticationSuccessHandler)//登陆成功后的handler
                .usernameParameter("loginName").passwordParameter("password")//登录所需要的接收的参数loginName作用户名,password作密码
                .and()//另外
                .logout().permitAll()//注销操作,所有用户
                .and()//
                .exceptionHandling().accessDeniedPage("/accessDenied");//指定异常处理界面

Spring Security核心类

Authentication

用来表示用户的认证信息,在登陆之前,会自动将信息封装成一个Authentication实现类对象,登录认证成功后产生成一个包含更多信息的Authentication对象,然后保存在SecurityContextHolder所持有的SecurityContext中,供后期调用

SecurityContext
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值