码迷,mamicode.com
首页 > 编程语言 > 详细

TZ_10_spring-sucrity 服务器和页面的权限控制

时间:2019-08-31 19:25:19      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:对象   orm   ring   cat   glob   disabled   类型   pid   findall   

1.在服务器端我们可以通过Spring security提供的注解对方法来进行权限控制。

Spring Security在方法的权限控制上支持三种类型的注解,JSR-250注解、@Secured注解和支持表达式的注解,这三种注解默认都是没有启用的,需要单独通过global-method-security元素的对应属性进行启用

 

  1>.JSR-250注解

   配置文件开启注解

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

@RolesAllowed表示访问对应方法时所应该具有的角色示例:
@RolesAllowed({"USER", "ADMIN"}) 该方法只要具有"USER", "ADMIN"任意一种权限就可以访问。这里可以省略前缀ROLE_,实际的权限可能是ROLE_ADMIN


只有带有ADMIN角色的用户才能访问
@RequestMapping("/findAllProduct")
@RolesAllowed("ADMIN") public ModelAndView findAllProduct(int page, int size) { ModelAndView mode = new ModelAndView(); List<Product> productList = productService.findAll(page,size); PageInfo pageInfo = new PageInfo(productList); mode.addObject("pageInfo", pageInfo); mode.setViewName("product-list"); return mode; }

 


2>@Secured注解

配置文件开启注解
 <security:global-method-security secured-annotations="enabled"/>
@Secured注解标注的方法进行权限控制的支持,其值默认为disabled
 只有带有ADMIN角色的用户才能访问
@RequestMapping("/findAllProduct")
    @Secured("ADMIN")
    public ModelAndView findAllProduct(int page, int size) {
        ModelAndView mode = new ModelAndView();
        List<Product> productList = productService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(productList);
        mode.addObject("pageInfo", pageInfo);
        mode.setViewName("product-list");
        return mode;
    }

 


2 页面端标签控制权限
jsp页面中我们可以使用spring security提供的权限标签来进行权限控制

1>maven
<spring.security.version>5.0.1.RELEASE</spring.security.version>
<
dependency>

    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
<version>version</version>
</
dependency>

 

2>页面标签导入
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
  
  标签: authentication 获得对象的属性
<security:authentication property="" htmlEscape="" scope="" var=""/>
  
  获得当前页面对象的名字
  <sec:authentication property="principal.username"/>

  
  标签:authorize 隐藏某一个对象
  当前用户携带的角色不是ADMIN是隐藏该标签
      <sec:authorize access="hasRole(‘ADMIN‘)">
              <a
                        href="${pageContext.request.contextPath}/User/findAllUser"> 
<i class="fa fa-circle-o"></i> 用户管理 </a> </sec:authorize>

 




 

TZ_10_spring-sucrity 服务器和页面的权限控制

标签:对象   orm   ring   cat   glob   disabled   类型   pid   findall   

原文地址:https://www.cnblogs.com/asndxj/p/11439916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!