0%

HTML 超链接定位(锚点链接)详解:用法、场景与进阶技巧

HTML 超链接定位(又称 “锚点链接”)是通过 a 标签的 href 属性结合目标元素的 id(或 name)属性,实现 “页面内精准跳转” 或 “跨页面指定位置跳转” 的功能。其核心价值在于提升长页面的导航体验(如文档目录跳转、表单错误定位)和跨页面精准访问(如直接跳转到目标页面的某一章节)。从 “核心原理→基础用法→进阶场景→注意事项” 四个维度,全面讲解锚点链接的使用方法。

核心原理

锚点链接的实现依赖两个核心部分:

  1. 触发链接:带有 href="#目标ID"a 标签(点击后触发跳转);
  2. 目标锚点:带有 id="目标ID"(或 name="目标ID",旧语法)的元素(跳转的终点位置)。

当用户点击触发链接时,浏览器会:

  • 解析 href 中的 #目标ID,查找当前页面(或目标页面)中 id 与 “目标 ID” 匹配的元素;
  • 将页面滚动到该元素的位置,使其处于可视区域内(通常是页面顶部或中部对齐)。

基础用法:页面内锚点定位

页面内锚点是最常用的场景,适用于长文档、长表单、多章节页面(如帮助文档、产品介绍页),实现 “目录→内容” 的快速跳转。

1. 标准语法(推荐用 id 定义锚点)

阅读全文 »

Nginx 状态检查配置:通过 stub_status 监控服务状态

Nginx 的stub_status模块提供了简单的状态检查功能,可实时查看服务器的连接数、请求处理情况等关键指标,是监控 Nginx 运行状态的常用工具。本文详细讲解stub_status的配置方法、状态指标含义及实战应用。

启用 stub_status 模块

stub_status是 Nginx 的非核心模块,默认不编译,需在编译 Nginx 时通过--with-http_stub_status_module参数启用。

  • 检查模块是否已安装
    执行nginx -V,若输出包含--with-http_stub_status_module,则模块已启用。

基础配置:暴露状态检查地址

通过location块配置状态检查路径(如/nginx_status),示例如下:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name example.com;

# 配置状态检查路径
location /nginx_status {
stub_status on; # 启用状态检查
allow 192.168.1.0/24; # 仅允许内部网段访问(安全加固)
deny all; # 拒绝其他IP访问
}
}

配置说明

  • stub_status on:开启状态检查功能,访问/nginx_status时返回状态信息;
  • allow/deny:限制访问来源(必填!避免状态信息暴露给公网,存在安全风险)。

状态信息详解

配置完成后,通过curl http://example.com/nginx_status访问,返回内容如下:

阅读全文 »

Spring Security 注解使用详解

在 Spring Boot 项目中,Spring Security 提供了丰富的注解来简化权限控制配置,无需大量 XML 或 Java 配置类即可实现精细的权限管理。使用前需先开启注解支持,再根据业务场景选择合适的注解进行权限控制。以下是完整的注解使用指南,包含开启方式、核心注解详解、使用场景及注意事项。

开启 Spring Security 注解支持

所有 Spring Security 方法级注解都需要通过 @EnableGlobalMethodSecurity 注解开启(Spring Security 5.6+ 推荐使用 @EnableMethodSecurity,功能更强大且支持 Lambda 表达式)。

传统方式:@EnableGlobalMethodSecurity(适用于旧版本)

该注解用于开启全局方法级安全控制,通过属性启用不同类型的注解,需添加在配置类上(如 SecurityConfig)。

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

// 开启 Web 安全 + 全局方法级安全注解
@EnableWebSecurity
@EnableGlobalMethodSecurity(
securedEnabled = true, // 开启 @Secured 注解支持
prePostEnabled = true, // 开启 @PreAuthorize/@PostAuthorize 注解支持
jsr250Enabled = true // 开启 JSR-250 注解(如 @RolesAllowed、@PermitAll)
)
public class SecurityConfig {
// 其他 Security 配置(如密码编码器、认证管理器等)
}

推荐方式:@EnableMethodSecurity(Spring Security 5.6+)

@EnableMethodSecurity@EnableGlobalMethodSecurity 的替代方案,默认开启 prePostEnabled = true,支持更简洁的配置和 Lambda 表达式,推荐在新版本中使用。

阅读全文 »

Spring Security 核心流程全解析:认证与权限控制的底层逻辑

Spring Security 的核心能力分为认证(Authentication)权限(Authorization) 两部分,分别对应 “确认用户身份” 和 “判断用户是否有权访问资源”。从 “请求拦截→认证处理→权限校验” 三个维度,逐行拆解流程中的关键组件与数据流转。

认证流程:从登录请求到身份确认

认证流程的核心是 “获取用户凭证→校验凭证→存储认证信息”,入口是 UsernamePasswordAuthenticationFilter(处理表单登录请求),最终通过 AuthenticationManagerUserDetailsService 完成校验。

1. 流程入口:UsernamePasswordAuthenticationFilter 拦截登录请求

UsernamePasswordAuthenticationFilter 是处理 POST /login(或自定义登录路径)请求的专用过滤器,继承自 AbstractAuthenticationProcessingFilter,其核心逻辑在父类的 doFilter() 方法中触发。

步骤 1:AbstractAuthenticationProcessingFilter#doFilter—— 过滤器核心调度

所有登录请求首先进入该方法,判断是否需要认证并触发后续逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

// 1. 判断请求是否需要认证(是否是登录请求、是否已认证)
if (!requiresAuthentication(request, response)) {
chain.doFilter(request, response);
return;
}

Authentication authResult = null;
AuthenticationException failed = null;

try {
// 2. 核心:尝试认证(委托给子类实现,即 UsernamePasswordAuthenticationFilter#attemptAuthentication)
authResult = attemptAuthentication(request, response);
} catch (AuthenticationException e) {
failed = e;
}

// 3. 认证成功:处理成功逻辑(存储认证信息、跳转)
if (authResult != null) {
successfulAuthentication(request, response, chain, authResult);
}
// 4. 认证失败:处理失败逻辑(清理上下文、跳转错误页)
else if (failed != null) {
unsuccessfulAuthentication(request, response, failed);
}
}
阅读全文 »

Spring Security 过滤 Web 请求全解析:从过滤器链到请求授权配置

Spring Security 对 Web 请求的过滤核心是 “代理过滤器 + 链式过滤” 机制:通过 DelegatingFilterProxy 衔接 Servlet 容器与 Spring 容器,再由 FilterChainProxy 管理具体的安全过滤链,最终通过 HttpSecurity 配置请求拦截规则。从 “过滤器链原理→自动注册机制→核心配置→请求授权规则” 四个维度,彻底讲透 Spring Security 如何过滤和保护 Web 请求。

核心过滤器链:DelegatingFilterProxy 与 FilterChainProxy

Spring Security 的过滤能力依赖两层代理机制,解决了 “Servlet Filter 由容器管理,而 Spring Security Filter 是 Spring Bean” 的协同问题。

1. DelegatingFilterProxy:Servlet Filter 与 Spring Bean 的桥梁

DelegatingFilterProxy 是 Servlet 规范中的 Filter 实现,但它本身不处理请求,仅负责将请求委托给 Spring 容器中的 FilterChainProxy Bean(默认 Bean 名为 springSecurityFilterChain)。

核心作用
  • 解耦容器与 Spring:Servlet 容器(如 Tomcat)只能识别并管理 Filter 实例,而 Spring Security 的过滤逻辑封装在 Spring Bean 中,DelegatingFilterProxy 作为中间代理,实现两者通信;
  • 延迟查找目标 Bean:初始化时不直接依赖 FilterChainProxy,而是在请求到来时从 Spring 容器中查找,支持 Spring Bean 的懒加载。
自动注册机制(AbstractSecurityWebApplicationInitializer)

用户提供的代码中,SecurityWebApplicationInitializer 继承 AbstractSecurityWebApplicationInitializer,Spring 会自动检测该类,并在 Servlet 容器启动时注册 DelegatingFilterProxy

阅读全文 »