Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

Java Spring Boot VS .NetCore (三)Ioc容器处理

Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (七) 配置文件

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

谈到安全,如现在市面上有的 OAuth2 \ OIDC -OpenId Connect ,身份认证、授权等,下面先来说下Java Security

这一块的东西非常多复杂,不能是Spring Security 还是 .NetCore Security,一点一点的比较说明

Spring Security

组成部分:

SecurityContextHolder, 提供几种访问 SecurityContext的方式。

SecurityContext, 保存Authentication信息和请求对应的安全信息。

Authentication, 展示Spring Security特定的主体。

GrantedAuthority, 反应,在应用程序范围你,赋予主体的权限。

UserDetails,通过你的应用DAO,提供必要的信息,构建Authentication对象。

UserDetailsService, 创建一个UserDetails,传递一个 String类型的用户名(或者证书ID或其他).

Spring Security 安全种的 SecurityContextHolder 对象 与 .NetCore中的 HttpContext上下对象 针对 Security这块  类似,当然.NetCore中的HttpContext 还有其他职责,这里就 HttpContext Authentication 说事

SecurityContextHolder:为我们提供了 获取 SecurityContext的上下文对象及策略相关,这里根据不同的策略获取获取到三种:

ThreadLocalSecurityContextHolderStrategy

InheritableThreadLocalSecurityContextHolderStrategy

GlobalSecurityContextHolderStrategy

当然也可以自定义策略处理,有单独的自定处理

else {
try {
Class<?> clazz = Class.forName(strategyName);
Constructor<?> customStrategy = clazz.getConstructor();
strategy = (SecurityContextHolderStrategy)customStrategy.newInstance();
} catch (Exception var2) {
ReflectionUtils.handleReflectionException(var2);
}

SecurityContext: 通过这个对象我们可以获取到 授权信息

SecurityContextHolder.getContext().getAuthentication()
public interface Authentication extends Principal, Serializable {
Collection<? extends GrantedAuthority> getAuthorities(); Object getCredentials(); Object getDetails(); Object getPrincipal(); boolean isAuthenticated(); void setAuthenticated(boolean var1) throws IllegalArgumentException;
}

这里就跟 .NetCore中的 HttpContext.User.Identity 身份信息一致

Spring中 Security getAuthentication 得到了授权身份信息,那么这个身份 有没有授权,是什么样的身份信息呢?这里都能得到相关的处理

那么获取想当前访问人的信息

Object principal=  SecurityContextHolder.getContext().getAuthentication().getPrincipal();

这里跟.NetCore  Authentication下的 方法类是 ,这个下面也封装了 Principal (ClaimsPrincipal 类型),当然对外部也提供了 那就是 User强转 ClaimsPrincipal

 public abstract Task<AuthenticateInfo> GetAuthenticateInfoAsync

看下.NetCore下面的强转:

  var user = HttpContext.User as ClaimsPrincipal;

这点其实在Spring 里面也存在这个处理 看到 getPrincipal() 获取去当事人信息的时候得到的是 Object对象 并不是 UserDeatils这个 对象

所以 Spring Security 里面 也有这么一出

Object principal=  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
String username = ((UserDetails)principal).getUsername();
} else {
String username = principal.toString();
}

这里跟.NetCore中的扩展登录信息一样 需要处理 当事人的身份信息,这我用.NeCore中 Windows 身份当事人信息来举例子

if (result?.Principal is WindowsPrincipal wp)
{ id.AddClaim(new Claim(JwtClaimTypes.Subject, wp.Identity.Name));
}

这一点跟上面的Spring Security 是同样的原理

.NetCore

首先抛开Session这种登录处理,这里介绍的是 Authentication认证,下面简单介绍下

AuthenticationBuilder :创建认证
AuthenticationSchemeOptions :认证的参数
AuthenticationHandler :认证处理
AuthenticationMiddleware : 认证中间件

.NetCore下 首先

添加认证服务给出参数

services.AddAuthentication(
options =>
{
options.DefaultScheme = "Cookies";
// options.DefaultChallengeScheme = "oidc"; })

然后添加授权认证的中间件,说有授权都是中间件来处理,这里可以去看中间件的原理,处理完成后会把信息写入HttpContext上下文对象中的身份认证信息,同时暴露对HttpContext的安全访问

 app.UseAuthentication();

代码中通过 SignInAsync、SignOutAsync 处理 (这里是异步) 这些方法暴露给了Httpcontext 同时也暴露给了 AuthenticationManager  对象

SignIn 会把通过本地验证后的信息写入认证相关的对象中,同时中间件对HttpContext上下问提供安全访问

所以在代码中我们一般这样处理:这里提供认证管理 只读的安全访问对象操作

public abstract AuthenticationManager Authentication { get; }

同时还扩展暴露了 身份信息

public abstract ClaimsPrincipal User { get; set; }

这个玩意是用来干什么的呢?其实就是为了我们获取认证的身份信息

可以看下这个下面的身份信息,下面有IsAuthenticated 、Name 、AuthenticationType

HttpContext.User.Identity

IsAuthenticated :这个用户的身份 是否认证

Name: 这个用户的身份 是谁 是哪个人

AuthenticationType:身份类型

这一篇就说道这里,可能说的不够详细~

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security的更多相关文章

  1. Spring Boot 2.X(九):Spring MVC - 拦截器(Interceptor)

    拦截器 1.简介 Spring MVC 中的拦截器(Interceptor)类似于 Servlet 开发中的过滤器 Filter,它主要用于拦截用户请求并作相应的处理,它也是 AOP 编程思想的体现, ...

  2. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  4. spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?

    spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的? 消息中间件在解决异步处理,模块间解耦和,和高流量场景的削峰,等情况下有着很广泛的应用 . 本文将跟大家一起 ...

  5. Spring Boot 2 (三):Spring Boot 2 相关开源软件

    Spring Boot 2 (三):Spring Boot 2 相关开源软件 一.awesome-spring-boot Spring Boot 中文索引,这是一个专门收集 Spring Boot 相 ...

  6. Spring Boot 2(一):Spring Boot 2.0新特性

    Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...

  7. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  8. spring boot 入门一 构建spring boot 工程

    最近在学习Spring boot,所以想通过博客的形式和大家分享学习的过程,同时也为了更好的学习技术,下面直接进入Spring boot的世界. 简介 spring boot 它的设计目的就是为例简化 ...

  9. spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin

    参考:Spring Boot Admin 2.0 上手 Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提 ...

  10. 【自学Spring Boot】什么是Spring Boot

    为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...

随机推荐

  1. Win10 + Ubuntu双系统,删除Ubuntu系统

    之前在电脑上装了win10 + ubuntu的双系统,偶尔会出问题,所以还是选择将ubuntu系统删掉. 正所谓“请神容易送神难”,安装ubuntu的时候,过程还算顺利,但是在删除Ubuntu的过程中 ...

  2. Python量化交易

    资料整理: 1.python量化的一个github 代码 2.原理 + python基础 讲解 3.目前发现不错的两个量化交易 学习平台: 聚宽和优矿在量化交易都是在15年线上布局的,聚宽是15年的新 ...

  3. 基于maven的spring-boot的pom文件详解

    Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-boot-s ...

  4. Vivado寄存器初始值问题

    前言 本复位只针对Vivado中的寄存器复位. 什么时候需要复位?到底要不要复位?怎么复位?复位有什么卵用? 该复位的寄存器需要复位,复位使得寄存器恢复初始值,有的寄存器并不需要复位(数据流路径上). ...

  5. emwin 之模态窗口

    @2019-02-27 [小记] emwin 窗口被模态之后,创建子窗口则原模态窗口变为非模态

  6. Wannafly挑战赛23 T2游戏 SG函数

    哎,被卡科技了,想了三个小时,最后还是大佬给我说是\(SG\)函数. \(SG\)函数,用起来很简单,证明呢?(不可能的,这辈子都是不可能的) \(SG\)定理 游戏的\(SG\)函数就是各个子游戏的 ...

  7. python之OpenCv(三)---基本绘图

    opencv 提供了绘制直线.圆形.矩形等基本绘图的功能 1.绘直线 cv2.line(画布,起点坐标,终点坐标,颜色,宽度) 例如: cv2.line(image,(20,60),(300,400) ...

  8. Windows启动过程(MBR引导过程分析)

    catalogue . 电脑启动过程 . MBR分析(master boot record) - 位于整个硬盘的 扇区 . DBR(DOS boot record) - 位于柱面0,磁头1,扇区1,即 ...

  9. tolua之wrap文件的原理与使用

    什么是wrap文件 每个wrap文件都是对一个c#类的包装,在lua中,通过对wrap类中的函数调用,间接的对c#实例进行操作. wrap类文件生成和使用的总体流程 生成一个wrap文件的流程 这部分 ...

  10. 第十一节:Bundles压缩合并js和css及原理分析

    一. 简介 1.背景:浏览器默认一次性请求的网络数是有上限的,如果你得js和css文件太多,就会导致浏览器需要多次加载,影响页面的加载速度, MVC中提供Bundles的方式压缩合并js和css,是M ...