Spring Security 中的权限控制方式 实现权限管理方法级别权限管理 配置方案与注解 页面端标签控制权限

wewodianizxmehg@[toc](Spring security权限控制)

一、服务器端方法级别权限控制

spring security在方法权限控制上支持三种注解 JSR-250、Secured注解、支持表达式注解,默认情况加这三种注解都没有开启。

1.1 JSR-250

1.1.1 导入jsr250-api

在这里插入图片描述

1.1.2 注解开启

在这里插入图片描述

1.1.3 在方法当中添加注解

  • @RolesAllowed
    • 配置,在访问的时候,应该具有的角色!
  • 基本案例:对于findAll方法,限制只能带有ADMIN角色的进行访问
    @RolesAllowed("ADMIN")
    @RequestMapping("/findAllByPage.do")
    public ModelAndView findAllUserByPages(@RequestParam(name = "curPage",defaultValue = "1")int curPage,@RequestParam(name = "size",defaultValue = "5")int size) throws Exception{
    
    
        List<UserInfo> allUsers = userService.findAllUsersByPage(curPage,size);
        //使用分页对象封装
        PageInfo<UserInfo> pageInfo = new PageInfo<>(allUsers);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userPage",pageInfo);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

如果权限控制不起作用,那么添加AOP事务支持就可以了: 方法权限的控制,都是基于AOP的

<!--
    支持AOP的注解支持,AOP底层使用代理技术
    JDK动态代理,要求必须有接口
    cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
-->
<aop:aspectj-autoproxy proxy-target-class=“true”></aop:aspectj-autoproxy>

1.2 Secured配置

1.2.1 注解支持开启

<security:globle-method-security secured-annotations="enabled"/>

1.2.2 在方法上使用

	@RequestMapping("/findAllOrdersByPage.do")
    @Secured({
    
    "ROLE_ADMIN"})
    public ModelAndView findAllByPage(@RequestParam(name = "curPage",defaultValue = "1") int curPage,@RequestParam(name = "size",defaultValue = "5")int size)throws Exception{
    
    
        //查询所有
        List<Orders> allOrdersByPage = ordersService.findAllOrdersByPage(curPage, size);
        //分页对象
        PageInfo<Orders> pageInfo = new PageInfo<>(allOrdersByPage);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("orders-list");
        return modelAndView;
    }

注意:在该注解使用时候,不能省略前缀;而JSR250可以省略前缀

1.3 支持SPEL表达式

1.3.1 注解支持开启

<security:global-method-security pre-post-annotations="enabled" />

1.3.2 在方法之上使用

  • 使用SPEL表达式
    在这里插入图片描述
    在这里插入图片描述

更加灵活强大,可以使用表达式能实现更多灵活的!


二、页面端标签控制权限

2.1 前言说明

  • Spring security提供了使用标签获取用户信息
  • spring security可以通过标签对页面元素进行访问控制
  • 由于再此处我们会用到SPEL,所以我们需要开启支持
 <security:http auto-config="true" use-expressions="true">

2.2 获取用户信息

  1. 在获取用户信息之前我们需要先导入Spring security的EL支持库
	 <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-taglibs</artifactId>
           <version>${spring-sercuirty.version}</version>
       </dependency>
  1. 关于用户信息的分析
    • 查询数据库后返回对象,封装到User当中
    • principal(主角)就是User对象。
    • spring会将principal封装到Authentication(认证)对象中
    • Authentication(认证) 会被封装给SecurityContext当中
    • 最后将SecurityContext对象保存到当前线程
  2. 获取对象名称
SecurityContextHolder.getContext().getAuthentication().getName()
  1. 控制元素:如果没有响应角色,则隐藏
<security:authorize access="hasRole('ROLE_ADMIN')">
		<li id="system-setting"><a
				href="${pageContext.request.contextPath}/role/findAllRoles.do"> <i
				class="fa fa-circle-o"></i> 角色管理
		</a>
		</li>
	</security:authorize>

猜你喜欢

转载自blog.csdn.net/Janyi_/article/details/114368554
今日推荐