谈到安全,如现在市面上有的 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 extendsPrincipal, Serializable {

Collection extends GrantedAuthority>getAuthorities();

Object getCredentials();

Object getDetails();

Object getPrincipal();booleanisAuthenticated();void setAuthenticated(boolean var1) throwsIllegalArgumentException;

}

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

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

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

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

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

public abstract Task GetAuthenticateInfoAsync

看下.NetCore下面的强转:

var user = HttpContext.User as ClaimsPrincipal;

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

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

Object principal=SecurityContextHolder.getContext().getAuthentication().getPrincipal();if (principal instanceofUserDetails) {

String username=((UserDetails)principal).getUsername();

}else{

String username=principal.toString();

}

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

if (result?.Principal is WindowsPrincipal wp)

{

id.AddClaim(newClaim(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:身份类型

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

.netcore 和 java_Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security相关推荐

  1. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  2. Spring Boot (一)Spring Boot 概述

    Spring Boot(一) 一 . Spring Boot 是什么? 首先Spring Boot不是一个框架,它是一种用来轻松创建具有最小或零配置的独立应用程序的方式.这是方法用来开发基于Sprin ...

  3. Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源

    Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源 在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spri ...

  4. spring boot测试_测试Spring Boot有条件的合理方式

    spring boot测试 如果您或多或少有经验的Spring Boot用户,那么很幸运,在某些时候您可能需要遇到必须有条件地注入特定bean或配置的情况 . 它的机制是很好理解的 ,但有时这样的测试 ...

  5. Spring Boot Admin –用于管理Spring Boot应用程序的Admin UI

    作为微服务开发的一部分,我们许多人都将Spring Boot与Spring Cloud功能一起使用. 在微服务领域,我们将有许多Spring Boot应用程序将在相同/不同的主机上运行. 如果将Spr ...

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

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

  7. (转)Spring Boot 2 (八):Spring Boot 集成 Memcached

    http://www.ityouknow.com/springboot/2018/09/01/spring-boot-memcached.html Memcached 介绍 Memcached 是一个 ...

  8. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  9. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot--2分钟构建spring web mvc REST风格HelloWorld 之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介 ...

  10. boot入门思想 spring_什么是Spring boot?Spring Boot快速入门以及Spring Boot实例教程

    转自:http://www.jianshu.com/p/d24bceea7665 简介 在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候, ...

最新文章

  1. tensorrt动态输入分辨率尺寸
  2. 人工机器:人工智能中的机器学习方法
  3. 分布式任务调度平台一站式讲解
  4. 不采取任何措施 盒盖_得了癌症如果不化疗能活多久?医生的答案很实在
  5. php 添加水印, 格式转换, 变换大小 Watermark, png2jpg, resize
  6. Linux下main函数启动过程【程序员自我修养笔记】【自用】
  7. [nRF51822] 13、浅谈nRF51822和NRF24LE1/NRF24LU1/NRF24L01经典2.4G模块无线通信配置与流程...
  8. 超分20220225讨论
  9. php transform,css transform属性怎么用
  10. 计算机在线考试摘要,基于WEB的网络在线考试系统-毕业论文中文摘要题目(可编辑).doc...
  11. Go基础编程:工作区
  12. access mysql odbc驱动程序_access odbc驱动下载|
  13. Amlogic Linux系列(四) 视频解码分析2
  14. 【论文笔记】Enhancing Adversarial Example Transferability with an Intermediate Level Attack
  15. 在BOSS直聘发现了一个前端小秘密
  16. 亚商投资顾问 早餐FM/1028华为海外推广5.5G
  17. java生成ca证书_生成CA根证书的脚本
  18. 【XBEE手册】XBEE操作
  19. Rust AES加密、解密工具
  20. libvirt numatune 原理

热门文章

  1. Pspice 使用指南(中文)
  2. halcon学习之颜色与纹理
  3. 用c语言的输入,用C语言输入的“%p”是什么意思?
  4. Mysql多源复制半同步_MySQL多源复制搭建
  5. 虚拟机提示找不到引导映像_从ISO镜像启动虚拟机,安装OS时提示no usable disks have been foun...
  6. 安装mysql查看随机密码命令_centos7/8 yum安装mysql8并查看默认root密码
  7. jsp+左间距_交互规范:栅格系统让页面元素间距更统一
  8. 关于python字符编码_关于python文件的字符编码
  9. matlab拉普拉斯算子边缘提取_使用平面光学器件进行图像边缘的差分检测
  10. Vue实例-本地留言板