页面端标签控制权限

在jsp页面中,可以使用spring security提供的权限标签来进行权限控制

第一步:pom.xml文件中引入依赖

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

第二步:在jsp页面中导入

<%@taglib uri="http://www.springframework.org/security/tags" prefix="security"%>

第三步:在页面上使用
常用标签:
authentication:可以获取当前登陆用户的信息
例如:在我的项目中,页面头部(header.jsp)和侧栏(aside.jsp)中需要显示登陆者的username
执行完以上操作后,把原始的xxx改为

<security:authentication property="principal.username"/>

就能显示

authorize:用于控制页面上哪些标签可以显示
例如,我的侧栏(aside.jsp)中有一个角色管理标签,需要只对拥有ADMIN权限的用户可见,则可以使用:<security:authorize access="/>

<li id="system-setting">
	<security:authorize access="hasRole('ROLE_ADMIN')">
	<a
	href="${pageContext.request.contextPath}/role/findAll.do"> <i
		class="fa fa-circle-o"></i> 角色管理
	</a>
	</security:authorize>
</li>

但这样会把报错,原因是在spring-security.xml中有:

<security:http auto-config="true" use-expressions="false">
 <security:intercept-url pattern="/**" access="Role_User,ROLE_ADMI"/>

这意味着不能使用表达式,改为

 <security:http auto-config="true" use-expressions="true">
        <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

注:‘ROLE_USER’,'ROLE_ADMIN’一定要大写,否则会报错

成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值