spring security 中的授权使用

2024-09-08 03:44

本文主要是介绍spring security 中的授权使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、认证

    身份认证,就是判断一个用户是否为合法用户的处理过程。Spring Security 中支持多种不同方式的认证,但是无论开发者使用那种方式认证,都不会影响授权功能使用。因为 SpringSecurity 很好做到了认证和授权解耦。

 

二、授权

    授权,即访问控制,控制谁能访问哪些资源。简单的理解授权就是根据系统提前设置好的规则,给用户分配可以访问某一个资源的权限,用户根据自己所具有权限,去执行相应操作。

2.1权限管理核心概念

     我们得知认证成功之后会将当前登录用户信息保存到Authentication 对象中,Authentication 对象中有一个getAuthorities() 方法,用来返回当前登录用户具备的权限信息,也就是当前用户具有权限信息。该方法的返回值为 Collection<?extends GrantedAuthorit,当需要进行权限判断时,就回根据集合返回权限信息调用相应方法进行判断。

2.2 GrantedAuthority 解释

 

    那么问题来了,针对于这个返回值 GrantedAuthority 应该如何理解呢? 是角色还是权限?

我们针对于授权可以是 基于角色权限管理 和 基于资源权限管理 ,从设计层面上来说,角色和权限是两个完全不同的东西: 权限是一些具体操作,角色则是某些权限集合。如:READ_BOOK 和 ROLE_ADMIN 是完全不同的。因此至于返回值是什么取决于你的业务设计情况:。基于角色权限设计就是: 用户 => 角色 => 资源  三者关系 返回就是用户的 角色。基于资源权限设计就是:用户e=>双限<=>资源”三者关系 返回就是用户的 权限。基于角色和资源权限设计就是: 用户角色<>权限<>资源 返回统称为用户的 权限为什么可以统称为权限,因为从代码层面角色和权限没有太大不同都是权限,特别是在Spring Security中,角色和权限处理方式基本上都是一样的。唯一区别 SpringSecurity在很多时候会自动给角色添加一个 ROLE_前缀,而权限则不会自动添加。

 

2.3 权限管理策略

 

Spring Security 中提供的权限管理策略主要有两种类型:

1)、基于过滤器(URL)的权限管理(FilterSecurityinterceptor)

           基于过滤器的权限管理主要是用来拦截 HTTP 请求,拦截下来之后,根据 HTTP 请求地址进行权限校验。

2)、基于AOP 的权限管理 (MethodSecurityinterceptor)

        基于AOP 权限管理主要是用来处理方法级别的权限问题。当需要调用某一个方法时,通过AOP 将操作拦截下来,然后判断用户是否具备相关的权限。

2.3.1 基于URL 权限管理

  在配置中写死,/** 需要有xx角色或者权限才能访问

2.3.1.1 准备工作

 

 1)pom.xml 依赖包

 2)测试controller

 3)security 配置

我们在controller中创建了三个方法,分别为

/adminInf   这个url(可以匹配 /adminInf.   /adminInf/  /adminInf.htm 等等)需要拥有admin的角色才能访问

/rootInf  这个url 需要拥有root的角色才能访问

/getUser 这个url 需要拥有read:user的权限才能才能访问

2.3.1.2 security 配置

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {/***  定义自己的userDetail**/@Beanpublic UserDetailsService userDetailsService() {InMemoryUserDetailsManager im = new InMemoryUserDetailsManager();im.createUser(User.withUsername("admin").password("{noop}123").roles("admin","root").build());im.createUser(User.withUsername("root").password("{noop}123").roles("root").build());im.createUser(User.withUsername("test").password("{noop}123").authorities("read:user").build());return im;}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService());}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().mvcMatchers("/adminInf").hasRole("admin") // /adminInf 这个url下面必须有 admin的角色才能访问.mvcMatchers("/rootInf").hasAnyRole("admin","root") // /rootInf 这个url下面 有 admin 或者root 角色均可以访问.mvcMatchers("/getUser").hasAnyAuthority("read:user") // /getUser 这个url 下面必须有 read:user 这个权限才能访问.anyRequest().authenticated().and().formLogin()// 开启form表单登录.and().csrf().disable();}
}

2.3.1.3 测试controller

@RestController
public class HelloController {@GetMapping("/getUser")public String getUser() {return "userinfo authority ok ";}@GetMapping("/adminInf")public String admin() {return "admin role ok ";}@GetMapping("/rootInf")public String root() {return "root role ok ";}
}

2.3.1.4 测试结果

         1)、当我们登录admin 的用户时候访问,因为getUser没有配置权限,所以不能访问

7af3a7a0ad2445a788f6fd7da722af03.png

e8c37ed31867412381b5aa506fb8d49e.png

6328b9110ff344729794be73d6e45e7c.png

        2)、当我们登录test 的用户时候访问,因为rootinfo/ adminInfo没有配置角色,所以不能访问

ef84193f4c9943e8bccf36aa7c176174.png

cb7f6258d8b8449cb8c2f6e7d7c533aa.png

f687ec1ae1984c9c8b19236bb5624725.png

 

 

2.3.1.5 基于多种匹配规则

 

MvcMatchersAuthorizedUrl 、mvcMatchers 基于mvc 的匹配规则

 /test     可以匹配  /test.   /test/   /test.h...   多种

 

org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry#antMatchers(java.lang.String...)

.antMatchers()  早期 4.0 之前使用,基于全路径匹配

 /test     只能匹配  /test  这个路径下的方法

 

从用法上来看两个使用基本没有区别,区别主要是在于匹配的路径上,mvc 可以匹配范围更广,ant 是全匹配

org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry#regexMatchers(java.lang.String...)  基于正则方案,当我们写一个正则表达式就可以

049d8c078fb14b1ab376643dbf426180.png

 


2.4 基于方法的权限管理

     基于方法的权限管理主要是通过AOP 来实现的,Spring Security 中通过

MethodSecuritvInterceptor 来提供相关的实现。不同在于Filter Security interceptor 只是在请

求之前进行前置处理,MethodSecuritvinterceptor 除了前置处理之外还可以进行后置处理。

前置处理就是在请求之前判断是否具备相应的权限,后置处理则是对方法的执行结果进行二

次过滤。前置处理和后置处理分别对应了不同的实现类。

2.4.1 开启注解支持

@EnableGlobalMethodSecurity

3aeb81066f054d8aaf1d654c270da102.png

1)、perPostEnabled: 开启 Spring Security 提供的四个权限注解,@PostAuthorize

@PostFilter、@PreAuthorize 以及 @PreFilter。

2)、securedEnabled: 开启 Spring Security 提供的 @Secured 注解支持,该注解不支持权限表达式

3)、jsr250Enabled:开启JSR-250 提供的注解,主要是@DenyAll、@PermitAll、@RolesAll 同

样这些注解也不支持权限表达式

# 以上注解含义如下:

- @PostAuthorize: 在日标方法执行之后进行权限校验。

- @PostFiter: 在目标方法执行之后对方法的返回结果进行过滤。

- @PreAuthorize: 在目标方法执行之前进行权限校验。

- @PreFiter: 在日标方法执行之前对方法参数进行过滤

- @secured: 访问目标方法必须具各相应的角色

- @DenyA11: 拒绝所有访问。

- @PermitA1l: 允许所有访问。

- @RolesAllowed: 访问目标方法必须具备相应的角色

这些基于方法的权限管理相关的注解,一般来说只要设置 prePostEnabled=true 就够用了

 

2.4.2 权限表达式

e064275f9bcf42cca6becb50a9b74289.png

 

 

2.4.3 角色权限实战

1) 

    /**

     *  登录用户必须是  admin  而且角色必须是 ADMIN

     * @return

     */

    @PreAuthorize("hasRole('ADMIN') and authentication.name == 'admin'")

    @RequestMapping("hello")

    public String hello() {

        return "hello";

    }

4cf981fe5feb47709b465c8fb01cbfc8.png


2)、

    /**

     *  登录的用户名必须和传过来的用户名一致才能通过  spe 表达式

     * @param username

     * @return

     */

    @PreAuthorize("authentication.name == #username")

    @RequestMapping("username")

    public String username(String username) {

        return "hello:" + username;

}

e005bcd5189f4673b03211a13f6e2bbc.png


3)、

    /**

     *  过滤 users 对象里面的属性  id % 2 的数据,保留 不能整除的

     *  users 必须是一个集合,否则没法过滤  filterObject 固定写法

     * @param users

     * @return

     */

    @PreFilter(value = "filterObject.id % 2 != 0",filterTarget = "users")

    @RequestMapping("users")

    public String addUser(@RequestBody List<SecurityUser> users) {

        System.out.println(users);

        try {

            String userStr = new ObjectMapper().writeValueAsString(users);

            return userStr;

        } catch (JsonProcessingException e) {

            e.printStackTrace();

        }

        return "null ";

}

9b91e1103d1c441d809c55b398d144ed.png

ab343a982db24d99a87a88aa6a00fab7.png


4、    

   /**

     *  后置过滤   当请求过来的  id值为1的时候,那么就返回,否则就不返回

     * @param id

     * @return

     */

    @PostAuthorize(value = "returnObject.id ==1 ")

    @RequestMapping("getUserId")

    public SecurityUser getUserId(Integer id) {

       return new SecurityUser(id,"lq");

}

98e1c1e7d27d4822acc3725a15235a6d.png


5)

   /** 

     *  保留  id % 2 ==0 的数据返回,用来对方法返回值进行过滤

     * @return

     */

    @PostFilter(value = "filterObject.id %2==0 ")

    @RequestMapping("getAllUser")

    public List<SecurityUser> getAllUser() {

        List<SecurityUser> userList = new ArrayList<>();

        IntStream.rangeClosed(0,10)

                .forEach(index -> userList.add(new SecurityUser(index,"lq-"+index)));

 

        return userList;

 

    }

96722f9266e34d858930921018521df1.png

jsr250 使用比较少的,因为功能比较单一

/***  只能判断角色,而且需要自己加前缀  ROLE_     当前用户必须有 ADMIN 权限才能查看* @return*/
@Secured({"ROLE_ADMIN"})
@RequestMapping(value = "getUser1")
public SecurityUser getUser1() {return new SecurityUser(1,"lisi");
}/***  判断用户有  ADMIN或者 ROOT 角色就可以访问* @return*/
@Secured({"ROLE_ADMIN","ROLE_ROOT"})
@RequestMapping(value = "getUser2")
public SecurityUser getUser2() {return new SecurityUser(1,"王五");
}/***  所有的用户都可以访问* @return*/
@PermitAll
@RequestMapping(value = "permitAll")
public String perAll() {return "permitAll ok ";
}/***  所有的用户都拒绝访问* @return*/
@DenyAll
@RequestMapping(value = "denyAll")
public String denyAll() {return "denyAll ok ";
}/***  判断用户有  ADMIN或者 ROOT 角色就可以访问* @return*/
@RolesAllowed({"ROLE_ADMIN","ROLE_ROOT"})
@RequestMapping(value = "rolesAllowed")
public String rolesAllowed() {return "rolesAllowed ok ";
}

 

 

三、授权原理分析

 

 3.1 AccessDecisionManager

(访问决策管理器),用来决定此次访问是否被允许

56edef3ce0334557a7c3117da53dae06.png

 

3.2 AccessDecisionVoter

(访问决定投票器),投票器会检查用户是否具备应有的角色,进而投出赞成、反对或者弃权票。

6b041841af084d20b4e3994166f1a07d.pngAccesDecisionVoter和AccessDecisionManager 都有众多的实现类,在 AccessDecisionManager 中会换个遍历 AccessDecisionVoter,进而决定是否允许用户访问,因而 AaccesDecisionVoter 和 AccessDecisionManager 两者的关系类似于 AuthenticationProvider 和ProviderManager 的关系。

 

3.3 ConfigAttribute

用来保存授权时的角色信息

b1b94722d7614961859ae149b489faaf.png

   在 Spring Security 中,用户请求一个资源(通常是一个接口或者一个 Java 方法)需要的角色会被封装成一个 ConfigAttribute 对象,在configAttribute 中只有一个 getAttribute方法,该方法返回一个 Strng 字符串,就是角色的名称。一般来说,角色名称都带有一个 ROLE_前缀,投票器 AccessDecisionVoter 所做的事情,其实就是比较用户所具各的角色和请求某个资源所需的 ConfigAtuibute 之间的关系。

3.4 核心类 FilterSecurityInterceptor

db245317e7e84a7fbbf82ffca734c891.png

 

3.4.1 源码

        

org.springframework.security.web.access.intercept.FilterSecurityInterceptor

        org.springframework.security.web.access.intercept.FilterSecurityInterceptor#invoke

                         org.springframework.security.access.intercept.AbstractSecurityInterceptor#beforeInvocation

                        org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource#getAttributes

org.                                springframework.security.access.intercept.AbstractSecurityInterceptor#attemptAuthorization

9a89057254494904b0d84888dd82a459.png

 

3.4.2  SecurityMetadataSource

后期可以实现这个类,自定义过滤规则,我们下一章讲解动态从数据库如何配置,以及重写这个类的实现        

fa1358e18d9b48f485f7ce35b0670f85.png

47688253bce94646aca4434e0c253e21.png

 


 

四、我们自己的角色如何放进去

我们通过参考

org.springframework.security.core.userdetails.User 类中的roles方法

0a0e211df1084fe99693b5c30ffa5892.png

 

4.1 代码实现

 // 获取权限信息 todo:后期从数据库查询

List<String>perList=Arrays.asList("new:query", "news:delete"); 

#角色 我们将这两个角色加上前缀

List<String>roles=Arrays.asList("ADMIN","USER");

List<String>roleList=roles.stream().map(r ->"ROLE_"+ r).collect(Collectors.toList());

 perList.addAll(roleList);

 

LoginSessionUserInf loginSessionUserInf=new LoginSessionUserInf(tUserInf, perList);

89717c79b7ca45228114e35641e4f94f.png

 

这篇关于spring security 中的授权使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147017

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

Security OAuth2 单点登录流程

单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性。当拥有这项属性时,当用户登录时,就可以获取所有系统的访问权限,不用对每个单一系统都逐一登录。这项功能通常是以轻型目录访问协议(LDAP)来实现,在服务器上会将用户信息存储到LDAP数据库中。相同的,单一注销(single sign-off)就是指

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na