springSecurity 基于方法的细粒度权限控制(动态代理)

springSecurity
基于方法的细粒度权限控制(动态代理)

1导入jar包

javax.annotation
jsr250-api
1.0

2.security.xml文件中配置

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

3.在方法上添加注解
@RolesAllowed

@RequestMapping("/findAll.do")
@RolesAllowed(“ADMIN”)//jsr-250 注解 只有ADMIN用户才可以访问
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
List productList = ps.findAll();
mv.addObject(“productList”,productList);
mv.setViewName(“product-list”);
return mv;
}

@secured
1.开启使用

<security:global-method-security secured-annotations=“enabled”/>

2.再方法上使用注解
@Secured(“ROLE_ADMIN”) //在使用JSR250注解时,可以省略ROLE_前缀,而使用@Secured是不能省略前缀的

当同事开启@Secured和JSR250注解时需要这么配置
<security:global-method-security jsr250-annotations=“enabled” secured-annotations=“enabled”/>

页面端进行权限控制
1.导入依赖 jar包

org.springframework.security
spring-security-taglibs
${spring.security.version}

2.在相应的jsp界面导入标签
<%@taglib uri=“http://www.springframework.org/security/tags” prefix=“security”%>
3.在页面上使用
authentication 可以获取当前用户的信息
property: 只允许指定Authentication所拥有的属性,可以进行属性的级联获取,如“principle.username”,
不允许直接通过方法进行调用

获取用户的信息
<security:authentication property=“principal.username”></security:authentication>

authorize: 用于控制页面上某些标签是否可以使用
<security:authorize access=“hasRole(‘ADMIN’)”>
用户管理

</security:authorize>

4,使用在security.xml配置或者设置webexpressionHandler的bean

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

<security:intercept-url pattern="/**" access=“hasAnyRole(‘ROLE_USER’,‘ROLE_ADMIN’)”/>

或者

<bean id="webexpressionHandler" 
		class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />

不开启设置一个bean就可以了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值