Web浏览器

网址就是请求服务器上对应的文件
DNS从顶级域名开始根据网址查IP。DNS服务器通过缓存查过的IP来加快服务响应(缓存拥有有效期)。
浏览器调用Socket库建立连接管道

TCP/IP协议栈

应用将数据分割为许多个网络包,TCP加上头部发送出去。需要发送的信息会缓存起来,超出计时时间或者数据量大于一个包大小时才发出去。
TCP需要确认对方收到数据,但是为了不浪费时间,实际上TCP在等待确认信息的过程中也在发送包,这个方法称为滑动窗口
为了使接收方有足够的时间处理数据,接收方有一个接收缓存,当缓存满了会在TCP头部中通知发送方;处理完成,缓冲区有空余时也会通过TCP头部通知发送方。
通信完成后,发送方会保留套接字一段时间,以防对方的FINISH信号发送有延迟,把新的相同端口的套接字错误删除。

用电信号传输数据时,用高电平表示1,低电平表示0.为了防止连续的低电平读不出来,会有一条时钟信号(固定切换高低电平)
ICMP:定义各种消息,如超时、无法到达、超出转发能力

UDP在发送的数据少于一个包(重发也只需要发一个包,代价小,如DNS查询控制信息)、发送的数据具有时效性(如音视频,必须快速发送,即使重发也没有意义)的情况下使用。UDP只负责收发数据,对方没收到就全部重发一遍。

1.0 Session

有状态:用户请求接口 -> 从Session中读取用户信息 -> 根据当前的用户来处理业务 -> 返回

缺点:不支持分布式

2.0 Token

无状态:用户携带Token请求接口 -> 从请求中获取用户信息 -> 根据当前的用户来处理业务 -> 返回

<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.3.0</version>
</dependency>
  • 工具类
public class JwtUtils {
//Jwt秘钥
private static final String key = "abcdefghijklmn";

// 根据用户信息创建Jwt令牌
public static String createJwt(UserDetails user){
Algorithm algorithm = Algorithm.HMAC256(key);
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
calendar.add(Calendar.SECOND, 3600 * 24 * 7);
return JWT.create()
.withClaim("name", user.getUsername()) // 配置JWT自定义信息
.withClaim("authorities", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).toList())
.withExpiresAt(calendar.getTime()) // 设置过期时间
.withIssuedAt(now) // 设置创建创建时间
.sign(algorithm); // 最终签名
}

// 根据Jwt验证并解析用户信息
public static UserDetails resolveJwt(String token){
Algorithm algorithm = Algorithm.HMAC256(key);
JWTVerifier jwtVerifier = JWT.require(algorithm).build();
try {
DecodedJWT verify = jwtVerifier.verify(token); // 对JWT令牌进行验证,看看是否被修改
Map<String, Claim> claims = verify.getClaims(); // 获取令牌中内容
if(new Date().after(claims.get("exp").asDate())) // 如果是过期令牌则返回null
return null;
else
// 重新组装为UserDetails对象,包括用户名、授权信息等
return User
.withUsername(claims.get("name").asString())
.password("")
.authorities(claims.get("authorities").asArray(String.class))
.build();
} catch (JWTVerificationException e) {
return null;
}
}
}
  • Filter
public class JwtAuthenticationFilter extends OncePerRequestFilter {  
// 继承OncePerRequestFilter表示每次请求过滤一次,用于快速编写JWT校验规则

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// 首先从Header中取出JWT
String authorization = request.getHeader("Authorization");
// 判断是否包含JWT且格式正确
if (authorization != null && authorization.startsWith("Bearer ")) {
String token = authorization.substring(7);
// 开始解析成UserDetails对象,如果得到的是null说明解析失败,JWT有问题
UserDetails user = JwtUtils.resolveJwt(token);
if(user != null) {
// 验证没有问题,那么就可以开始创建Authentication了,这里我们跟默认情况保持一致
// 使用UsernamePasswordAuthenticationToken作为实体,填写相关用户信息进去
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
// 然后直接把配置好的Authentication塞给SecurityContext表示已经完成验证
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
// 最后放行,继续下一个过滤器
// 可能各位小伙伴会好奇,要是没验证成功不是应该拦截吗?这个其实没有关系的
// 因为如果没有验证失败上面是不会给SecurityContext设置Authentication的,后面直接就被拦截掉了
// 而且有可能用户发起的是用户名密码登录请求,这种情况也要放行的,不然怎么登录,所以说直接放行就好
filterChain.doFilter(request, response);
}
}
  • Security修改为无状态
// 将Session管理创建策略改成无状态,这样SpringSecurity就不会创建会话了,也不会采用之前那套机制记录用户,因为现在我们可以直接从JWT中获取信息
.sessionManagement(conf -> {
conf.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
})
// 添加我们用于处理JWT的过滤器到Security过滤器链中,注意要放在UsernamePasswordAuthenticationFilter之前
.addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)

JWT退出登录

采用黑名单方案。一台服务器存储JWT黑名单,共享给所有微服务。

JWT.create()
// 额外添加一个UUID用于记录黑名单,将其作为JWT的ID属性jti
.withJWTId(UUID.randomUUID().toString())
public class JwtUtils {    

private static final HashSet<String> blackList = new HashSet<>();
// 加入黑名单方法
public static boolean invalidate(String token){
Algorithm algorithm = Algorithm.HMAC256(key);
JWTVerifier jwtVerifier = JWT.require(algorithm).build();
try {
DecodedJWT verify = jwtVerifier.verify(token);
Map<String, Claim> claims = verify.getClaims();
//取出UUID丢进黑名单中
return blackList.add(verify.getId());
} catch (JWTVerificationException e) {
return false;
}
}

public static UserDetails resolveJwt(String token){
Algorithm algorithm = Algorithm.HMAC256(key);
JWTVerifier jwtVerifier = JWT.require(algorithm).build();
try {
DecodedJWT verify = jwtVerifier.verify(token);
// 判断是否存在于黑名单中,如果存在,则返回null表示失效
if(blackList.contains(verify.getId()))
return null;
Map<String, Claim> claims = verify.getClaims();
if(new Date().after(claims.get("exp").asDate()))
return null;
return User
.withUsername(claims.get("name").asString())
.password("")
.authorities(claims.get("authorities").asArray(String.class))
.build();
} catch (JWTVerificationException e) {
return null;
}
}
}

Chapter2 指令:计算机的语言

本章将介绍MIPS汇编语言指令。

三条设计原则

  1. 简单源于规整 Simplicity favors regularity.
  2. 越小越快 Smaller is faster.
  3. 优秀的设计需要适宜的折中方案 Good design demands good compromises.

2.2 硬件的操作与操作数

规整

add a, b, c // a = b + c MIPS汇编语言使用这样的固定记法。
每条MIPS算术指令只执行1个操作,仅有3个变量。

操作数必须来自寄存器

变量f、g、h、i、j依次分配给$s0~$s4,编译下面的C语句

f = (g + h) - (i + j);
---
add $t0, $s1, $s2 // t0 = s1 + s2
add $t1, $s3, $s4
sub $s0, $t0, $t1 // s0 = t0 + t1

数据传输

只有少量数据存在寄存器中,因此需要在存储器和寄存器间传输数据

A的基址是存在$s3,编译下面的C语句

A[12] = h + A[8]
---
lw $t0, 32($s3) // 先读数,再相加;32为偏移量,8*4byte
add $t0, $s2, $t0
sw $t0, 48($s3) // 存数

立即数

addi $t0, $t1, 4 // t0 = t1 + 4;无需读取4,作为立即数相加
subi $t0, $t1, 4
阅读全文 »

策略模式

class MallardDuck extends Duck {
public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
}

class ModelDuck extends Duck {
public ModelDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyNoWay(); // 组合不同的方法
}
}

class Main {
public static void main(String[] args) {
Duck real = new MallardDuck();
Duck model = new ModelDuck();

real.fly();
model.fly(); // 调用同样的接口
}
}

识别应用中变化的方面,把它们和不变的方面分开。

针对接口编程,而不是针对实现编程。

// Implement
Dog d = new Dog();
d.bark();

// Interface
Animal dog = new Dog();
dog.makeSound(); // abstract

优先使用组合而不是继承。

summary

策略模式定义了算法族并分别封装。策略让算法变化独立于使用它的客户。

阅读全文 »

微服务

微服务:解决接口越来越多,单体应用运行缓慢问题。

踩坑记录

找不到Mapper

***************************
APPLICATION FAILED TO START
***************************

Description:

Field deviceMapper in com.esagent.es.EsDataInit required a bean of type 'com.example.mapper.YourMapper' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.mapper.YourMapper' in your configuration.

原因是mybatis版本有问题!

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.4</version>
<!-- 版本需要和其他依赖对上 -->
</dependency>
</dependencies>
</dependencyManagement>

阅读全文 »

PA1-1 24.5.30

又开始了ICS之旅,这次又给自己下了一个难度,找到了汪亮老师讲解的ICS 5!

target

第一课的目标是修正一个register错误声明

insteresting

  • 中途网易源Bad Gateway 502了,更换清华源,学会了:%s/163/tuna/g非常爽!
  • 又学了几个终端快捷键
  • 想到了用 ccache 加速我的PA

problems

  1. unionstruct 的区别?
    unioin 在同一个内存空间中存储不同的数据类型。
    struct 则是同时存储不同的数据类型。
  2. 为什么要用 union?阅读i386手册
    2.3.1 General Registers
    As Figure 2-5 shows, the low-order word of each of these eight registers has a separate name and can be treated as a unit. This feature is useful for handling 16-bit data items and for compatibility with the 8086 and 80286 processors. The word registers are named AX, BX, CX, DX, BP, SP, SI, and DI.
    对于CPU来说,可以把AH AX AL看成单独的单元,拆分成小块。所以它们是共用关系。

PA1-2 ALU 24.6.5

target

实现ALU中的各类运算,包括设置标志位

knowledge

Appendix C

Name Function
CF Carry Flag ── Set on high-order bit carry or borrow; cleared otherwise.
PF Parity Flag ── Set if low-order eight bits of result contain an even number of 1 bits; cleared otherwise.
ZF Zero Flag ── Set if result is zero; cleared otherwise.
SF Sign Flag ── Set equal to high-order bit of result (0 is positive, 1 if negative).
OF
Overflow Flag ── Set if result is too large a positive number or too small a negative number (excluding sign-bit) to fit in destination operand; cleared otherwise.
阅读全文 »

Tips

  1. 使用反向代理时,git相关命令会出问题
  2. 实验前,创建新分支pa号,在其中做修改,完成以后合并pa到master中,并转回master分支

模板

# PA
## notification
-
## difficulties
1.
## summary

PA0

notification

  • 全程使用git
  • git commit --allow-empty可以使git空(无更改的)提交
阅读全文 »

ThinkPHP 6.0文档

Composer安装

composer create-project topthink/think tp
# tp是应用根目录名称,可以修改

访问

  1. localhost/tp/public/ 首页
  2. 首页/index.php/index controller下的Index类
  3. 首页/index.php/index/?s=hello/value 调用controller下Index类的hello方法,并传入参数value(URL兼容模式)
  4. 首页/index.php/hello_world 调用controller下HelloWorld类
  5. 首页/index.php/group.blog 调用controller/group下的Blog类

输出数组

$data = array('a' => 1, 'b' => 2, 'c' => 3);
return json($data);

开启调试

在应用根目录下新建.env文件

APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = username
PASSWORD = password
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

[LANG]
default_lang = zh-cn

数据库

$user = Db::connect('mysql')->table('tp_user')->select();
// 确定数据库和数据表
return json($user);

// 或者有Model User
$user = User::select();

Model

<?php
namespace app\model;

use think\Model;

class User extends Model
{
protected $connection = 'databaseName';
}

查询

Db::getLastSql();可以看上一条sql语句

一条数据

Db::table('tableName')->where('fieldName', fieldValue)->find(); // 查询不到返回null
// findFail在查询不到时抛出异常;findEmpty在查询不到时返回空数组

整个表(数据集)

Db::table('tableName')->select();
Db::name('tableNameWithoutPrefix')->select();

某条记录的字段值

Db::name('tableName')->where('id', $id)->value('field');

一列(键值对)

Db::name('name')->column('fieldAsValue', 'fieldAsKey');

插入

一条数据

// strict 表示强行插入
Db::name('user')->strict(false)->insert($data);
// 有相同主键则替换
Db::name('user')->replace()->insert($data);
// 根据主键自动判断
Db::name('user')->save($data);

多条数据

Db::name('user')->insertAll($data);

修改

Db::name('user')->where('id', 232)
->update([
// 下面是SQL表达式
'email' => Db::raw('UPPER(email)'),
'price' => Db::raw('price + 1'),
'status'=> Db::raw('status - 2')
]);

删除

// 根据主键删除多条数据
Db::name('user')->delete([48,49,50]);
// 匹配删除
Db::name('user')->where('id', 47)->delete();
// 全部删除
Db::name('user')->delete(true);

模板

composer require topthink/think-view

1.0 Bar 柱状图

from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(["衬衣", "羊毛衫", "西装", "裤子", "鞋子", "袜子"])
bar.add_yaxis("商家", [5, 20, 40, 10, 70, 90])
# 相当于
bar = (
Bar()
.add_xaxis(["衬衣", "羊毛衫", "西装", "裤子", "鞋子", "袜子"])
.add_yaxis("商家", [5, 20, 40, 10, 70, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66]) # 支持两个柱子同时生成
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题")) # 生成标题
)


bar.render("bar.html") # 渲染为 bar.html,默认是 render.html
  • 效果
阅读全文 »

Mysql体系结构

DB与Instance

DB:数据库可以是ibd文件、放在内存的文件,是物理操作系统文件或其他形式文件类型的集合。
Instance:Mysql数据库由后台线程及一个共享内存区组成。

数据库实例才是真正操作数据库文件的。在集群情况下,可能存在一个DB被多个Instance使用的情况。

Mysql被设计为单进程多线程,在OS上的表现是一个进程。

插件式表存储引擎

存储引擎基于表,而不是DB。

存储引擎对开发人员透明。

索引原理

MySQL使用的是B+树作为索引的数据结构

B树是一个分支内按顺序存放多个节点数据的数据结构;而B+树在此基础上,在分支内只存储索引,只在叶子节点存储数据(这样每一层可以存储更多索引,减少层数),并且在叶节点之间用指针互相连接,提高访问效率。

阅读全文 »
0%