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. 基于vue现有项目的服务器端渲染SSR改造

    前面的话 不论是官网教程,还是官方DEMO,都是从0开始的服务端渲染配置.对于现有项目的服务器端渲染SSR改造,特别是基于vue cli生成的项目,没有特别提及.本文就小火柴的前端小站这个前台项目进行 ...

  2. Codeforces Round #551 (Div. 2) 题解

    CF1153A 直接做啊,分类讨论即可 #include<iostream> #include<string.h> #include<string> #includ ...

  3. Scrapy 框架,爬虫文件相关

    Spiders 介绍 由一系列定义了一个网址或一组网址类如何被爬取的类组成 具体包括如何执行爬取任务并且如何从页面中提取结构化的数据. 简单来说就是帮助你爬取数据的地方 内部行为 #1.生成初始的Re ...

  4. emwin 之 GUI_MessageBox 阻塞特性

    2019-03-01 [小记] GUI_MessageBox 函数执行后必须手动点击关闭窗口,未关闭窗口前线程将阻塞在此处等待关闭窗口事件 [使用场景] 由于该函数不会产生任何消息, 所以可利用阻塞特 ...

  5. Linux系统诊断必备技能之二:tcpdump抓包工具详解

    一.简述 TcpDump可以将网络中传送的数据包完全截获下来提供分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句来帮助你去掉无用的信息. Linux作为网络服 ...

  6. 【ASP.NET】website转webapplication

    *以下操作都以VS2013为参考: #新建两种web项目 1.添加webapplication项目: 2.添加website项目: #比较两种web项目新建的webform页面的不同点: 1.文件目录 ...

  7. 金融量化分析【day113】:多因子选股

    一.什么是多因子选股 在股市中征战过的朋友们应该知道,股市之道无非三点.1择时,2选股,3 仓控.精通这三点中的任何一点,都足以在股市中所向披靡.但是精通二字何其艰难!!!矫情的话多不多说,咱们进入正 ...

  8. Maven 学习总结(三) 之 依赖管理

    聚合 为了要一次构建多个项目,而不是到每个模块目录下分别执行mvn命令.maven聚合这一特性就是为该需求服务的.为此我们需要创建一个额外的模块aggregator, 然后通过该模块构建整个项目的所有 ...

  9. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  10. 算法第四版Question

    1.ECLIPES标准输入流 ①Run As-->Run Configurations-->Commom-->Input File在Input File里面输入要读取的文本文件 这对 ...