spring security权限控制(方法级别+页面级别)注解简单使用

2022-08-10 13:57:27

一: 方法级别权限控制

  1. JSR-250注解
  • pom.xml文件中导入依赖jar包
<dependency><groupId>javax.annotation</groupId><artifactId>jsr250-api</artifactId><version>1.0</version></dependency>
  • spring-security.xml中开启JSR250注解
<security:global-method-securityjsr250-annotations="enabled"/>
  • 在指定的方法上使用
@RequestMapping("/findAll.do")//    jsr250的注解,只有USER权限的用户才能访问产品列表界面@RolesAllowed("USER")public ModelAndViewfindAll()throws Exception{
        ModelAndView mv=newModelAndView();
        List<Product> pl=  productService.findAll();
        mv.addObject("productList", pl);
        mv.setViewName("product-list1");return mv;}
  1. @Secured注解(spring security框架中自带的)
  • spring-security.xml中开启secured注解
<security:global-method-securitysecured-annotations="enabled"/>
  • 在指定的方法上使用(必须加上“ROLE_”前缀)
//    secured注解,只有USER权限的用户才能访问产品列表界面@Secured("ROLE_USER")public ModelAndViewfindAll()throws Exception{
        ModelAndView mv=newModelAndView();
        List<Product> pl=  productService.findAll();
        mv.addObject("productList", pl);
        mv.setViewName("product-list1");return mv;}
  1. 支持表达式注解
    @PreAuthorize在方法调用之前基于表达式的计算结果来限制对方法的访问
    @PostAuthorize允许方法调用,但是如果表达式计算结果为false,将抛出一个安全性异常
  • spring-security.xml中开启支持表达式注解
<security:global-method-securitypre-post-annotations="enabled"/>
  • 在指定的方法上使用
//    范例1//    支持表达式注解,只有拥有ROLE_USER角色的用户才能访问产品列表界面@PreAuthorize("hasRole('ROLE_USER')")public ModelAndViewfindAll()throws Exception{
        ModelAndView mv=newModelAndView();
        List<Product> pl=  productService.findAll();
        mv.addObject("productList", pl);
        mv.setViewName("product-list1");return mv;}//  范例2@RequestMapping("/save.do")//  当前登陆用户名为root或者拥有USER权限的用户才能新建产品,hasAuthority和hasRole差别在于有没有前缀'ROLE_'@PreAuthorize("authentication.principal.username='root' or hasAuthority('USER')")public Stringsave(Product product)throws Exception{
        productService.save(product);//        添加数据后刷新数据return"redirect:findAll.do";}

二: 页面端权限控制
authentication(身份验证) 可以获取当前正在操作的用户信息
authorize(授权) 用于控制页面上某些标签是否可以显示(比如页面上的某些功能不想让用户看到)

  • pom.xml导入依赖
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId><version>5.0.1.RELEASE</version></dependency>
  • spring-security.xml文件配置
<!--    添加该句,jsp页面中就能解析web安全EL表达式,
并且<security:http auto-config="true" use-expressions="false">中的use-expressions不用改为true了--><beanid="webexpressionHandler"class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/><!-- 如果use-expressions="true", 则下面语句应该作出修改--><!-- 作用:只有拥有USER权限或者ADMIN权限的人才能访问系统--><security:intercept-urlpattern="/**"access="ROLE_USER,ROLE_ADMIN"/><!-- 上面语句要修改为--><security:intercept-urlpattern="/**"access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
  • 在jsp界面导入标签
<%--页面端权限控制标签--%>
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags"%>

<%--在需要显示用户信息的地方使用类似如下语句--%><security:authenticationproperty="principal.username"/>

<%--控制用户管理功能只能让具有ADMIN权限的用户看到--%>
```xml<ulclass="treeview-menu">
<%--  USER带不带ROLE_前缀都可以,该句话的作用是用户管理标签只能具有USER权限的用户可见--%><security:authorizeaccess="hasRole('USER')"><liid="system-setting"><ahref="${pageContext.request.contextPath}/user/findAll.do"><iclass="fa fa-circle-o"></i> 用户管理</a></li></security:authorize><liid="system-setting"><ahref="${pageContext.request.contextPath}/role/findAll.do"><iclass="fa fa-circle-o"></i> 角色管理</a></li></ul>
  • 作者:黑夜中坚持
  • 原文链接:https://blog.csdn.net/qq_26496077/article/details/109478198
    更新时间:2022-08-10 13:57:27