天天看点

SpringSecurity授权管理介绍

权限管理的两大核心是:认证和授权,前面我们已经介绍完了认证的内容,本文就给大家来介绍下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在注解支持对应类或者方法上添加注解

创建相关的控制器

SpringSecurity授权管理介绍

Jsr250注解的使用

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>jsr250-api</artifactId>
  <version>1.0</version>
</dependency>      
SpringSecurity授权管理介绍

效果

SpringSecurity授权管理介绍

Spring表达式注解使用

SpringSecurity授权管理介绍
SpringSecurity授权管理介绍
SpringSecurity授权管理介绍

SpringSecurity提供的注解使用

SpringSecurity授权管理介绍
SpringSecurity授权管理介绍
SpringSecurity授权管理介绍

1.3 权限异常处理

 对于没有访问权限的操作,我们直接给一个403的系统错误页面,用户体验也太差了,这时我们可以通过自定义异常处理来解决

自定义错误页面

SpringSecurity授权管理介绍

方式一:在spring-security.xml配置文件中处理

SpringSecurity授权管理介绍
SpringSecurity授权管理介绍

方式二:编写通过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的标签库差不多

SpringSecurity授权管理介绍

大家要注意,标签管理仅仅是隐藏了页面,但并没有做权限管理,所以后台权限管理是必须的!