【第七篇】SpringSecurity中的权限管理

在这里插入图片描述

SpringSecurity中的权限管理

SpringSecurity是一个权限管理框架,核心是认证和授权,前面已经系统的给大家介绍过了认证的实现和源码分析,本文重点来介绍下权限管理这块的原理。

一、权限管理的实现

服务端的各种资源要被SpringSecurity的权限管理控制我们可以通过注解和标签两种方式来处理。

image.png

放开了相关的注解后我们在Controller中就可以使用相关的注解来控制了

/**
 * JSR250
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RolesAllowed(value = {"ROLE_ADMIN"})
    @RequestMapping("/query")
    public String query(){
        System.out.println("用户查询....");
        return "/home.jsp";
    }
    @RolesAllowed(value = {"ROLE_USER"})
    @RequestMapping("/save")
    public String save(){
        System.out.println("用户添加....");
        return "/home.jsp";
    }

    @RequestMapping("/update")
    public String update(){
        System.out.println("用户更新....");
        return "/home.jsp";
    }
}


/**
 * Spring表达式
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @PreAuthorize(value = "hasAnyRole('ROLE_USER')")
    @RequestMapping("/query")
    public String query(){
        System.out.println("用户查询....");
        return "/home.jsp";
    }
    @PreAuthorize(value = "hasAnyRole('ROLE_ADMIN')")
    @RequestMapping("/save")
    public String save(){
        System.out.println("用户添加....");
        return "/home.jsp";
    }

    @RequestMapping("/update")
    public String update(){
        System.out.println("用户更新....");
        return "/home.jsp";
    }
}


@Controller
@RequestMapping("/role")
public class RoleController {

    @Secured(value = "ROLE_USER")
    @RequestMapping("/query")
    public String query(){
        System.out.println("用户查询....");
        return "/home.jsp";
    }

    @Secured("ROLE_ADMIN")
    @RequestMapping("/save")
    public String save(){
        System.out.println("用户添加....");
        return "/home.jsp";
    }

    @RequestMapping("/update")
    public String update(){
        System.out.println("用户更新....");
        return "/home.jsp";
    }
}

然后在页面模板文件中我们可以通过taglib来实现权限更细粒度的控制

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>HOME页面</h1>
<security:authentication property="principal.username" />
<security:authorize a
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Security 是一个用于身份验证和授权的强大架。它提供了一套完整的认证和授权流程,以下是 Spring Security 的认证授权流程: 1. 用户提交登录请求,服务器接收到请求后,进入认证过滤器链。该过滤器链由多个过滤器组成。 2. 过滤器链的第一个过滤器是 UsernamePasswordAuthenticationFilter,它负责处理基于用户名和密码的认证方式。该过滤器将用户提交的用户名和密码封装到一个 Authentication 对象。 3. 过滤器链的下一个过滤器是 AuthenticationManager,它是 Spring Security 的核心接口。AuthenticationManager 负责验证 Authentication 对象的有效性。它会委托给一个或多个 AuthenticationProvider 来进行具体的认证操作。 4. AuthenticationProvider 是 AuthenticationManager 的实现类,它负责具体的认证逻辑。Spring Security 提供了多个内置的 AuthenticationProvider 实现,如 DaoAuthenticationProvider(使用数据库进行认证)、LdapAuthenticationProvider(使用 LDAP 进行认证)等。开发者也可以自定义 AuthenticationProvider。 5. 在认证过程,AuthenticationProvider 会根据用户提供的凭证(如用户名和密码)进行认证,并生成一个经过认证的 Authentication 对象。 6. 当 AuthenticationProvider 认证成功后,Authentication 对象将被返回给 AuthenticationManager,并最终返回给 UsernamePasswordAuthenticationFilter。 7. 在认证成功后,UsernamePasswordAuthenticationFilter 会生成一个包含用户身份信息的 Authentication 对象,并将其存储在 Spring Security 的上下文。 8. 接下来,授权过滤器链会根据请求的 URL 和用户的权限信息,决定是否允许用户访问该资源。授权过滤器链由多个过滤器组成,其最重要的是 AccessDecisionManager 和 AccessDecisionVoter。 9. AccessDecisionManager 是授权决策的核心接口,它负责根据用户的权限信息和资源的访问要求,决定是否允许用户访问该资源。AccessDecisionManager 可以配置多个 AccessDecisionVoter,用于进行具体的授权决策。 10. AccessDecisionVoter 是 AccessDecisionManager 的实现类,它是授权决策的具体实现Spring Security 提供了多个内置的 AccessDecisionVoter 实现,如 RoleVoter(基于角色的授权)、AuthenticatedVoter(基于认证状态的授权)等。开发者也可以自定义 AccessDecisionVoter。 以上就是 Spring Security 的认证授权流程。它通过一系列的过滤器和接口来实现用户认证和资源授权的功能,保障了应用程序的安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值