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

一: 方法级别权限控制

  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-security jsr250-annotations="enabled"/>
  • 在指定的方法上使用
    @RequestMapping("/findAll.do")
//    jsr250的注解,只有USER权限的用户才能访问产品列表界面
    @RolesAllowed("USER")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        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-security secured-annotations="enabled"/>
  • 在指定的方法上使用(必须加上“ROLE_”前缀)
//    secured注解,只有USER权限的用户才能访问产品列表界面
    @Secured("ROLE_USER")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        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-security pre-post-annotations="enabled"/>
  • 在指定的方法上使用
//    范例1
//    支持表达式注解,只有拥有ROLE_USER角色的用户才能访问产品列表界面
    @PreAuthorize("hasRole('ROLE_USER')")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        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 String save(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了-->
<bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />

<!-- 如果use-expressions="true", 则下面语句应该作出修改-->
<!-- 作用:只有拥有USER权限或者ADMIN权限的人才能访问系统-->
<security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>

<!-- 上面语句要修改为-->
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
  • 在jsp界面导入标签
<%--页面端权限控制标签--%>
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags"%>

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

<%--控制用户管理功能只能让具有ADMIN权限的用户看到--%>
```xml
<ul class="treeview-menu">
<%--  USER带不带ROLE_前缀都可以,该句话的作用是用户管理标签只能具有USER权限的用户可见--%>
	<security:authorize access="hasRole('USER')">
	<li id="system-setting">
		<a href="${pageContext.request.contextPath}/user/findAll.do"> <i class="fa fa-circle-o"></i> 用户管理
		</a>
	</li>
	</security:authorize>
	
	<li id="system-setting">
		<a href="${pageContext.request.contextPath}/role/findAll.do"> <i class="fa fa-circle-o"></i> 角色管理
		</a>
	</li>
</ul>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值