您的位置:首页 > 编程语言 > Java开发

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
  • 收藏
  • 分享
  • 文章举报
波波烤鸭 博客专家 发布了397 篇原创文章 · 获赞 1812 · 访问量 299万+ 他的留言板 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: