SpringSecurity服务器端方法级权限控制@RolesAllowed注解不起作用的解决

JSR-250的使用
1.在maven导入依赖
<dependency>
         <groupId>javax.annotation</groupId>
         <artifactId>jsr250-api</artifactId>
         <version>1.0</version>
</dependency>
2.开启注解

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

 <security:global-method-security  jsr250-annotations="enabled" />
3.在指定方法上使用
    /**
     * 查询所有用户
     * @return
     */
    @RequestMapping("/findAll.do")
    @RolesAllowed({"ROLE_ADMIN"})
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        List<UserInfo> users = service.findAll();
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            System.out.println(authority); //输出ROLE_USER
        }
        mv.addObject("userList",users);
        mv.setViewName("user-list");
        return  mv;
    }
2.出现的问题

该注解不起作用,该注解规定只有拥有ROLE_ADMIN权限才能访问该方法。但实际只要登陆的任意角色都可访问。

3.解决

在springmvc.xml中添加即可,开启对于AOP的支持

 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

但具体原因还不知道,继续研究。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值