微服务安全:实战安全攻防(8)

实战安全攻防



第一章:假通行证制作工坊

想象有个伪造高手试图仿造小区通行证。他可能会尝试以下手段:捡到别人的通行证直接使用(令牌窃取)、复制通行证上的公章图案(签名伪造)、用过期通行证修改日期(时效篡改)。在JWT安全领域,这些对应着三种典型攻击:

// 防御伪造令牌的验签配置
@Bean
public JwtDecoder jwtDecoder() {
   
    return NimbusJwtDecoder.withJwkSetUri("https://auth.yourdomain.com/.well-known/jwks.json")
        .jwtProcessorCustomizer(processor -> {
   
            // 防御1:强制使用RS256算法(防止算法降级攻击)
            processor.setJWSAlgorithmConstraints(
                new JWSAlgorithmConstraints(
                    JWSAlgorithm.Family.RSA,
                    JWSAlgorithm.RS256));
            
            // 防御2:验证关键字段存在性
            processor.setJWTClaimsSetVerifier((claims, context) -> {
   
                requireClaim(claims, "iss");
                requireClaim(claims, "exp");
                requireClaim(claims, "aud");
            });
        })
        .build();
}

// 令牌防盗用过滤器
@Component
public class TokenTheftFilter extends OncePerRequestFilter {
   
    @Override
    protected void doFilterInternal(HttpServletRequest request, 
                                  HttpServletResponse response, 
                                  FilterChain chain) {
   
        String token = extractToken(request);
        // 防御3:检查令牌是否来自常用设备
        if (token != null && !deviceService.verifyBinding(token, request)) {
   
            auditService.logSuspiciousActivity(request);
            throw new InvalidTokenException("检测到异常设备使用令牌");
        }
        chain.doFilter(request, response);
    }
}

这就像小区门禁系统升级了防伪技术:所有通行证必须使用物业特制印章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双囍菜菜

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值