SpringSecurity(十二)—-基于表达式的访问控制

SpringSecurity(十二)—-基于表达式的访问控制

1.access()方法使用

1)使用理由

之前学习的登录用户权限判断实际上底层实现都是调用access(表达式),我们可以通过access()实现和hasAuthority,hasRole等的权限控制完成相同的功能。

 public ExpressionUrlAuthorizationConfigurer<H>.ExpressionInterceptUrlRegistry hasAuthority(String authority) {
   
            return this.access(ExpressionUrlAuthorizationConfigurer.hasAuthority(authority));
        }

    private static String hasAuthority(String authority) {
   
        return "hasAuthority('" + authority + "')";
    }

Expression表达式如下
在这里插入图片描述
也可以在源码中查找

    private static String hasAnyRole(String... authorities) {
   
        String anyAuthorities = StringUtils.arrayToDelimitedString(authorities, "','ROLE_");
        return "hasAnyRole('ROLE_" + anyAuthorities + "')";
    }

    private static String hasRole(String role) {
   
        Assert.notNull(role, "role cannot be null");
        if (role.startsWith("ROLE_")) {
   
            throw new IllegalArgumentException("role should not start with 'ROLE_' since it is automatically inserted. Got '" + role + "'");
        } else {
   
            return "hasRole('ROLE_" + role + "')";
        }
    }

    private static String hasAuthority(String authority) {
   
        return "hasAuthority('" + authority + "')";
    }

    private static String hasAnyAuthority(String... authorities) {
   
        String anyAuthorities = StringUtils.arrayToDelimitedString(authorities, "','");
        return "hasAnyAuthority('" + anyAuthorities + "')";
    }

    private static String hasIpAddress(String ipAddressExpression) {
   
        return "hasIpAddress('" + ipAddressExpression + "')";
    }

2.使用自定义方法

虽然这里面已经包含了很多的表达式(方法)但是在实际项目中很有可能出现需要自己自定义逻辑的情况。
例如:实现判断登录用户是否具有访问当前URL权限

1)自定义service接口和实现类,在实现类实现判断逻辑

public interface MyService  {
   
    boolean hasPermission(HttpServletRequest request, Authentication authentication);
}
@Service
public class MyServiceImpl implements MyService {
   

    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
   
        Object principal = authentication.getPrincipal();
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        if(principal instanceof User){
   
            User user=(User)principal;
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            boolean contains = authorities.contains(new SimpleGrantedAuthority(requestURI));
            System.out.println(contains);
            return authorities.contains(new SimpleGrantedAuthority(requestURI));

        }

        return false;
    }
}

2)修改配置类

 http.authorizeRequests()
 .antMatchers("/testaccess").access("@myServiceImpl.hasPermission(request,authentication)")

3)编写控制器

    @RequestMapping("/testaccess")
    @ResponseBody
    public String toaccess(){
   
        return "testaccess";
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2352.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据结构学习

    数据结构学习

    2021年9月27日
    45
  • linux系统怎么利用LVM扩容

    linux系统怎么利用LVM扩容引言:在linux系统下,如果在虚拟机层面进行扩容,首先是挂载一块虚拟机硬盘,然后在linux系统底下去分区,然后对挂载到新的目录,但是,如果是对linux系统里面的文件目录本身进行扩容的话,只能使用LVM来进行扩容,本文将分两部分介绍,第一部分是如何创建LVM的分区,第二部分是如何对文件目录本身利用LVM来进行扩容:创建LVM分区:1如下图,我这里有一块硬盘已经分区,如果直接对这块硬盘创建LVM分区,会报错,因此,可以先清除掉这块硬盘的分区这里本身已经分了区,如果要使用这块硬盘进行L…

    2022年6月20日
    30
  • Dubbo架构(应用架构)

    一、整体框架1、Dubbo介绍ApacheDubbo是一款高性能、轻量级的开源JavaRPC框架。它有三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。1、Dubbo特点1、面向接口代理的高性能RPC调用:提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。2、智能负载均衡:内置多种负载均衡策略,智能感知下游节点健康状…

    2022年4月11日
    98
  • 利用网页内容监控来提升网站收录排名

    利用网页内容监控来提升网站收录排名我们做网站最主要的是提升流量来获取收益,流量高了,知名度也回相应的提升,从而获得的收益也越多。提升流量的关键是,内容、收录于排名。有大量高质量的收录内容,配合靠前的排名,流量自然就涨了。那么如何提升网站收录排名呢?web视界就在网站收录这点来给大家介绍。首先要区分网站是新站还是老站。一、新站 网站是新站,新站关键词排名是不稳定的,有的时候你可能会受到新站保护获取一些关键词排名,但是这…

    2022年7月17日
    15
  • C语言之strstr函数

    C语言之strstr函数【FROMMSDN&&百科】原型:char*strstr(constchar*str1,constchar*str2);#include找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。Returnsapointertothefirstoccurrence

    2022年6月25日
    28
  • 解Linux SSH命令大全,新手必看SSH命令

    解Linux SSH命令大全,新手必看SSH命令

    2021年9月22日
    49

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号