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


相关推荐

  • TIMER2.0_电磁感应原理

    TIMER2.0_电磁感应原理其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来实现,例如这样:Timertimer=newTimer();timer.schedule(newTimerTask(){ publicvoidrun(){ System.out.println(“abc”); }},200

    2022年10月23日
  • 隐马尔科夫模型(hmm)_隐马尔可夫模型是什么意思

    隐马尔科夫模型(hmm)_隐马尔可夫模型是什么意思https://www.cnblogs.com/skyme/p/4651331.html

    2022年9月30日
  • spring使用aspectj_你可知晓

    spring使用aspectj_你可知晓【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)http://blog.csdn.net/javazejian/article/details/54629058出自【zejian的博客】关联文章:关于SpringIOC(DI-依赖注入)你需要知道的一切关于SpringAOP(AspectJ)你该知晓的一切本篇是年后第一篇博文,由于博主用了不少

    2022年8月11日
  • 改变传统思维,抢占世界“汽车产业”制高点

    改变传统思维,抢占世界“汽车产业”制高点

    2022年3月13日
  • 论文讨论&&思考《Deformable Convolutional Networks》

    论文讨论&&思考《Deformable Convolutional Networks》这篇论文真是让我又爱又恨,可以说是我看过的最认真也是最多次的几篇paper之一了,首先deformableconv的思想我觉得非常好,通过end-to-end的思想来做这件事也是极其的makese

    2022年8月6日
  • ARM版Oracle安装包_如何把Linux移植到手机

    ARM版Oracle安装包_如何把Linux移植到手机  linux作为一款流行的嵌入式系统,目前已经有多种架构的MCU支持Linux移植,arm64就是其中一种。今天在这里想做一个笔记,记录一下完整的arm64移植过程。嵌入式Linux系统组成部分嵌入式Linux移植到开发板上时,主要有四个组成部分,下面一一列举。  在启动过程中,bootloader加载设备树文件(dtb),之后启动内核(Startkernel),进而加载根文件系统(debian或者ubuntu),最后进入系统。  那么我们所做的工作可以分为以下三部分:1、进行bootlo

    2022年9月17日

发表回复

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

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