SpringSecurity(1-security入门-基于内存+角色方式的认证和授权的学习)

一:什么是Spring Security
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。它的核心是一组过滤器链,不同的功能经由不同的过滤器。

二:认证授权功能实现

1,准备一个springCloud的工程,引入oAuth2的依赖

<!-- oAuth2 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-oauth2</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>

2,创建基于内存认证/授权配置类

@EnableWebSecurity
@Configuration
public class webSecurityConfig extends WebSecurityConfigurerAdapter {
   
    //密码编译比对方式
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
   
        return new BCryptPasswordEncoder();
    }
    //访问路径配置
    @Override
    protected void configure(HttpSecurity http) throws java.lang.Exception {
   
		//.hasAuthority : 基于资源授权  .hasRole:基于角色授权
        http.authorizeRequests()
        .antMatchers("/auth/hello").hasRole("GUEST")   //给访问的地址加上角色限制,只有指定的角色才能访问
        .antMatchers("/auth/home").hasRole("USER")
        .antMatchers("/auth/admin&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值