SpringSecurity授权管理介绍
2020-04-19 21:25
501 查看
权限管理的两大核心是:认证和授权,前面我们已经介绍完了认证的内容,本文就给大家来介绍下SpringSecurity的授权管理
一、注解操作
我们在控制器或者service中实现授权操作比较理想的方式就是通过相应的注解来实现。SpringSecurity可以通过注解的方式来控制类或者方法的访问权限。注解需要对应的注解支持,若注解放在controller类中,对应注解支持应该放在mvc配置文件中,因为controller类是有mvc配置文件扫描并创建的,同理,注解放在service类中,对应注解支持应该放在spring配置文件中。由于我们现在是模拟业务操作,并没有service业务代码,所以就把注解放在controller类中了。
1.1开启授权的注解支持
这里给大家演示三类注解,但实际开发中,用一类即可!
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd "> <!-- 配置扫描路径--> <context:component-scan base-package="com.dpb.security.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!-- 开启权限控制注解支持 jsr250-annotations="enabled"表示支持jsr250-api的注解,需要jsr250-api的jar包 pre-post-annotations="enabled"表示支持spring表达式注解 secured-annotations="enabled"这才是SpringSecurity提供的注解 --> <security:global-method-security jsr250-annotations="enabled" pre-post-annotations="enabled" secured-annotations="enabled" /> </beans>
1.2在注解支持对应类或者方法上添加注解
创建相关的控制器
Jsr250注解的使用
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
效果
Spring表达式注解使用
效果
SpringSecurity提供的注解使用
效果
1.3 权限异常处理
对于没有访问权限的操作,我们直接给一个403的系统错误页面,用户体验也太差了,这时我们可以通过自定义异常处理来解决
自定义错误页面
方式一:在spring-security.xml配置文件中处理
方式二:编写通过SpringMVC的异常处理机制
也可以通过我们在介绍SpringMVC时介绍的5中异常处理方式,此处大家自行完善。
方式三:web.xml文件中设置
<error-page> <error-code>403</error-code> <location>/403.jsp</location> </error-page>
二、标签操作
上面介绍的注解方式可以控制服务器的访问,但是我们在前端页面上也需要把用户没有权限访问的信息给隐藏起来,这时我们需要通过SpringSecurity的标签库来实现,具体如下
<%-- Created by IntelliJ IDEA. User: dengp Date: 2019/12/1 Time: 20:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%> <html> <head> <title>Title</title> </head> <body> <h1>home界面</h1><br> 当前登录账号:<br> <security:authentication property="principal.username"/><br> <security:authentication property="name"/><br> <form action="/logout" method="post"> <security:csrfInput/> <input type="submit"value="注销"> </form> <security:authorize access="hasAnyRole('ROLE_ADMIN')" > <a href="#">系统管理</a> </security:authorize> <security:authorize access="hasAnyRole('ROLE_USER')" > <a href="#">用户管理</a> </security:authorize> </body> </html>
用起来和我们前面介绍shiro的标签库差不多
大家要注意,标签管理仅仅是隐藏了页面,但并没有做权限管理,所以后台权限管理是必须的!
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- 授权管理-LDAP-介绍与环境搭建
- .NET软件授权管理解决方案Eziriz .NET IntelliLock控件详细介绍
- IntelliLock托管代码保护和许可授权管理系统软件详细介绍及下载
- 授权管理-LDAP-介绍与环境搭建
- linux 启动项管理,运行级别介绍、chkconfig管理工具使用方法、linux系统启动项说明
- [STF手机设备管理平台]连接其它操作系统上的安卓设备实操介绍
- 版本管理工具介绍——SVN篇(二)
- iMatrix平台核心功能—权限管理介绍
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- bobmazelin的流程管理系统介绍和概念性设计(三)
- 缺陷管理工具介绍
- 05、面包屑分类,商品的分类管理CRUD,运营商的springSecurity安全框架(配置文件中定义用户名和密码角色),商家注册以非加密方式登录
- 阿里云OTS(开放结构化数据服务)可视化管理工具的设计和功能介绍
- 版本管理工具介绍—Git篇
- 【中文】Joomla1.7扩展介绍之Projectfork(项目管理)
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台(C#开发)-背景介绍
- 全面介绍Windows内存管理机制及C++内存分配实例(一):进程空间
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- 如何取得通用权限管理控件授权(下载C#通用权限管理源码)
- 企业级集中身份认证及授权管理实践freeipa