资源服务器安全集成
第一章:楼栋门禁系统改造
在智慧社区中,每栋别墅的门禁系统就是微服务架构中的资源服务器。它们需要具备三个核心能力:
- 识别通行证真伪(JWT验签)
- 检查通行权限范围(Scope验证)
- 处理异常情况(令牌过期/吊销)
我们先配置基础门禁设备:
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {
// 安装标准门禁设备
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// 所有请求默认需要通行证
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
// 启用JWT门禁系统
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(jwt -> jwt.decoder(jwtDecoder()))
// 添加异常处理装置
.exceptionHandling(eh -> eh
.authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
.accessDeniedHandler(new BearerTokenAccessDeniedHandler()));
return http.build();
}
// 配置公章验证器(公钥验签)
@Bean
JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withPublicKey(loadRsaPublicKey())
.signatureAlgorithm(SignatureAlgorithm