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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CodeBlocks-20.03下载安装及中文教程

    CodeBlocks-20.03下载安装及中文教程CodeBlocks-20.03下载安装及中文教程

    2022年7月26日
    5
  • mysql系列:全网最全索引类型汇总「建议收藏」

    mysql系列:全网最全索引类型汇总「建议收藏」前言除了常见的普通索引,唯一索引,组合索引,大家还能说一下mysql中有哪些其他类型的索引吗?今天和大家一起总结mysql中有哪些索引类型。一、mysql中有哪些索引类型?聚簇索引(ClusteredIndex)非聚簇索引主键索引(PRIMARYKEY)辅助索引(SecondaryIndexes)HASH索引BTREE索引T-TREE索引R-Tree索引自适应hash索引(AdaptiveHashIndex)唯一索引(UNIQUEIndexs)普通索引(No

    2022年5月22日
    32
  • linux+shell脚本100,shell脚本(shell编程100例)

    linux+shell脚本100,shell脚本(shell编程100例)ShellScript,Shell脚本与Windows/Dos下的批处理类似,也便是用各类指令预先放入到一个文件中,便利一次性执行的一个程序文件,主要是便利办理员进行设置或许办理用的。可是它比Windows下的批处理更强大,比用其他编程程序修改的程序功率更高,它使用了Linux/Unix下的指令。shell编程100例1、编写helloworld脚本#!/bin/bash#编写helloworld…

    2022年10月3日
    0
  • Ubuntu创建软连接[通俗易懂]

    Ubuntu创建软连接[通俗易懂]Ubuntu创建软连接建立软连接ln-s原目录删除软连接sudorm或者强制删除-rf创建软连接ln-s源地址目的地址列如:Ubuntu文件系统rootfs_dir软连接到/home/lp目录下ln-s/opt/Linux/root_dir/home/lp/roo_dir就OK了…

    2022年9月30日
    0
  • MySQL EXPLAIN type类型说明[通俗易懂]

    MySQL EXPLAIN type类型说明[通俗易懂]EXPLAIN执行计划中type字段分为以下几种:ALL    INDEX    RANGE    REF    EQ_REF    CONST,SYSTEM    NULL自上而下,性能从最差到最好 type=ALL,全表扫描,MYSQL扫描全表来找到匹配的行(因为film表中rating不是索引)mysql&gt;explainexten…

    2022年10月10日
    0
  • 最新超详细虚拟机VMware安装Kali Linux

    最新超详细虚拟机VMware安装Kali Linux作者:seriouszyx独立博客记录了日常学习总结代码均可在Github上找到(求Star)本文讲解如何在虚拟机上安装KaliLinux,希望对大家有所帮助。准备:一台电脑,VMware(VMware安装教程)一、下载系统镜像文件1.首先下载系统镜像,进入kali官网,在Downloads中选择DownloadKaliLinux,如下图所示。…

    2022年4月30日
    167

发表回复

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

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