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)
上一篇 2020年11月12日 上午9:48
下一篇 2020年11月12日 上午9:48


相关推荐

  • windows各版本序列号集合

    windows各版本序列号集合因经常使用,避免每次都上网到处找,在此做了集合(不定期更新)windows2003R2Sp264位企业版MR78C-GF2CY-KC864-DTG74-VMT73windows2003R232位企业版JCDPY-8M2V9-BR862-KH9XB-HJ3HMwindows764位旗舰版使用激活工具,附件1,出处:http://www.xpg…

    2022年7月20日
    18
  • BeanCopier类[通俗易懂]

    BeanCopier类[通俗易懂]网上学习了一番BeanCopier类。cglib是一款比较底层的操作java字节码的框架。下面通过拷贝bean对象来测试BeanCopier的特性:publicclassOrderEntity{privateintid;privateStringname;//Gettersandsettersareom…

    2025年9月13日
    7
  • 扫雷小游戏(简易版)

    扫雷小游戏(简易版)写个小游戏娱乐娱乐 目录 1 简要分析 2 实现扫雷游戏细节 3 总代码 1 简要分析一般写这种小游戏要分为两个模块 头文件 源文件 头文件中声明各种函数和库函数以及宏定义 全局变量等 源文件中主要是实现函数和逻辑函数等 在扫雷游戏中 我将我的模块分为三部分 一部分是 game h 声明头文件等 game c 实现游戏函数 test c 实现总体逻辑接下来就是扫雷游戏的细节处理 2 游戏细节 1 定义两个数组 charmi

    2026年3月18日
    2
  • 43、java.beans.PropertyDescriptor类

    43、java.beans.PropertyDescriptor类一、软件包java.beans    包含与开发beans有关的类,即基于JavaBeansTM架构的组件 二、PropertyDescriptor   PropertyDescriptor描述JavaBean通过一对存储器方法导出的一个属性 publicclassPropertyDescriptorextendsFeature…

    2026年4月15日
    4
  • cultureinfo 类 java_为国家,语言组合创建自定义CultureInfo

    cultureinfo 类 java_为国家,语言组合创建自定义CultureInfo我正在开发一个.net4.5应用程序,需要多语言支持多文化等.以下是国家/语言的示例列表俄罗斯/俄罗斯比利时/法国比利时/荷兰对于上述所有内容,可以根据上述文化名称创建CultureInfo对象ru-RUfr-BEnl-BE当用户进入站点时,我将Thread.CurrentThread.CurrentCulture和Thread.CurrentThread.CurrentUICulture设置…

    2022年6月19日
    26
  • vscode 前端最佳插件配置

    vscode最佳配置配置说明详解vscode配置内容在最后,已附上editor是针对vscode的风格设置例如tabSize:一个tab等于2个空格,行高为24pxworkbench是针对vscode的主题设置例如iconTheme(图标风格):使用插件vscode-great-icons(需搜索安装)例如color…

    2022年4月6日
    68

发表回复

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

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