Spring-security中控制方法访问权限注解的使用

JSR-250的介绍

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

JSR-250的三种注解:

  1. @RolesAllowed:表示访问对应方法时所应具有的角色
    示例:@RolesAllowed({“User”,“ADMIN”}) 该方法只要具有"User","Admin"任意一种权限就可以访问。这里可以省略前缀ROLE_,实际的权限可能是ROLE_ADMIN
  2. @PermitAll表示允许所有的角色进行访问,也就是说不进行权限的控制
  3. @DenyAll和@PermitAll相反的,表示无论什么角色都不能访问

使用步骤

第一步:在spring-security.xml中开启

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

第二步:导入maven依赖

<!--jsr250-->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

第三步:在指定的方法上添加注解

表示当前用户只有具备ADMIN权限才可以访问这个方法,否则会报403访问权限不足的错误
注:ADMIN前面是有个ROLE_前缀的,在这可以省略。

	@RequestMapping("/findAll.do")
    @RolesAllowed({"ADMIN"})
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4") int size) throws Exception {
        ModelAndView mv=new ModelAndView();
        List<Product> ps=productService.findAll(page,size);
        PageInfo pageInfo=new PageInfo(ps);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("product-list");
        return mv;
    }

另外在web.xml中可以配置403出错的页面

<error-page>
    <error-code>403</error-code>
    <location>/403.jsp</location>
  </error-page>

@Secured注解的使用

第一步:在spring-security文件中开启注解

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

第二步:在对应的方法上添加注解

注意点:与jsr-250不同的是注解中的ROLE_前缀是不可以省略的!

@RequestMapping("/findAll.do")
    @Secured({"ROLE_ADMIN"})
    public ModelAndView findAll(@RequestParam(name = "page", defaultValue = "1",required = true) int page,@RequestParam(name = "size",defaultValue = "4",required = true) int size) throws Exception {
        ModelAndView mv=new ModelAndView();
        List<Order> orderList=orderService.findAll(page,size);
        PageInfo pageInfo=new PageInfo(orderList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }

表达式使用的介绍

第一步:在spring-security中开启配置

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

第二步:使用,可以使用SPEL表达式

注意点:使用SPEL表达式,需要在spring-security.xml文件中将

<security:http auto-config="true" use-expressions="true">

中的use-expressions设为true

	@RequestMapping("/save.do")
    @PreAuthorize("authentication.principal.username=='吴许东'")//表示只有用户名为吴许东的用户可以访问
    public String save(UserInfo userInfo){
        userService.save(userInfo);
        return "redirect:findAll.do";

    }

	@RequestMapping("/findAll.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")//表示具有user角色的用户可以访问
    public ModelAndView findAll(@RequestParam(name="page",required = true, defaultValue = "1") int page,@RequestParam(name="size",required = true,defaultValue = "4") int size){
        ModelAndView mv=new ModelAndView();
        List<UserInfo> userInfoList =userService.findAll(page, size);
        PageInfo pageInfo=new PageInfo(userInfoList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("user-list");
        return mv;
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值