Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
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的更多相关文章
- Spring Boot 2.X(九):Spring MVC - 拦截器(Interceptor)
拦截器 1.简介 Spring MVC 中的拦截器(Interceptor)类似于 Servlet 开发中的过滤器 Filter,它主要用于拦截用户请求并作相应的处理,它也是 AOP 编程思想的体现, ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?
spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的? 消息中间件在解决异步处理,模块间解耦和,和高流量场景的削峰,等情况下有着很广泛的应用 . 本文将跟大家一起 ...
- Spring Boot 2 (三):Spring Boot 2 相关开源软件
Spring Boot 2 (三):Spring Boot 2 相关开源软件 一.awesome-spring-boot Spring Boot 中文索引,这是一个专门收集 Spring Boot 相 ...
- Spring Boot 2(一):Spring Boot 2.0新特性
Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- spring boot 入门一 构建spring boot 工程
最近在学习Spring boot,所以想通过博客的形式和大家分享学习的过程,同时也为了更好的学习技术,下面直接进入Spring boot的世界. 简介 spring boot 它的设计目的就是为例简化 ...
- 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 的基础上提 ...
- 【自学Spring Boot】什么是Spring Boot
为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...
随机推荐
- python之旅九【第九篇】socket
什么是socket 建立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket ...
- wiki
GRANT ALL PRIVILEGES ON confluence.* TO 'confluence'@'localhost' IDENTIFIED BY '%SaRK%TDpU#CyT6i';
- poj 1015 Jury Compromise(背包变形dp)
In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...
- JS学习笔记Day10
一.设置或获取元素对象中(标签中)的属性和自定义属性 对象.属性 对象['属性'] 对象.getAttribute('属性名') 对象.setAttribute('属性名','属性值'); 对象.re ...
- prometheus 配置介绍
prometheus 配置介绍 prometheus 配置分global.alerting.rule_files.scrape_configs 1.global(全局配置) scrape_interv ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- SOC(网络安全管理平台)
SOC平台,网络安全管理平台. 提供集中.统一.可视化的安全信息管理,通过实时采集各种安全信息,动态进行安全信息关联分析与风险评估,实现安全事件的快速跟踪.定位和应急响应.从监控.审计.风险和运维四个 ...
- AngularJS DI(依赖注入)实现推测
AngularJS DI(依赖注入) http://www.cnblogs.com/whitewolf/archive/2012/09/11/2680659.html 回到angularjs:在框架中 ...
- git的使用 (一)
1.版本控制 版本控制(Version Control Systems)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.这个系统可以自动帮我们备份文件的每一次更改,并且可以非常方 ...
- MySQL学习4 - 数据类型一
介绍 一.数值类型 二.浮点型 验证三种类型建表 验证三种类型的精度 三.日期类型 综合练习: 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选 ...