
基于spring jwt通常用于cloud的微服务架构(json web token)通过api进行用户认证和授权 gateway作为统一入口。然而,开发者正在实现用户注册(signup)和登录(login)当公共接口无需认证即可访问时,经常会遇到“full authentication is required to access this resource“错误。这通常发生在auth 直接在service内部要求这些接口,或者通过apice 当gateway转发请求时。同时,通过api gateway访问时,可能会出现“could not send request“连接错误往往是上游服务(auth service)由于拒绝申请认证问题,网关无法获得响应。
造成这个问题的原因是Spring Security的默认行为。没有明确的配置,Spring Security默认保护所有端点,并要求所有请求进行认证。对于用户注册和登录等接口,应在用户未经认证的情况下访问,因此需要特殊处理。
核心解决:Spring Security配置解决这个问题的关键是正确配置Auth Springng在Service中 Security,明确指定哪些路径可以在没有认证的情况下访问。这通常是通过使用authorizerequests()和permitall()在安全配置类别中实现的。
以下是修正后的Spring Security配置示例:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable() // 禁用CSRF,因为JWT是无状态的,
.authorizeRequests(auth -> {
// 定义公共访问路径
auth.antMatchers("/authenticate/signup", "/authenticate/login", "/authenticate/refreshtoken").permitAll();
// 所有其他要求都需要认证
auth.anyRequest().authenticated();
})
// 可根据需要添加JWT过滤器等
// .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class)
;
}
}
代码解析:
- http.csrf().disable(): CSRF禁用(Cross-Site Request Forgery)保护。使用JWT的无状态API通常不需要CSRF保护,因为JWT本身包含认证信息,不使用基于会话的CSRF令牌。
- authorizeRequests(auth -> { ... }): 这是配置请求授权规则的核心部分。
- auth.antMatchers("/authenticate/signup", "/authenticate/login", "/authenticate/refreshtoken").permitAll(): 这是解决问题的关键。它明确指定了/authenticate/signup(注册)、/authenticate/login(登录)和/authenticate/refreshtoken所有用户(包括未认证用户)都可以访问这些路径(刷新令牌)。permitAll()该方法允许所有请求访问这些路径,无需任何认证或授权。
- auth.anyRequest().authenticated(): 放行特定公共路径后,本规则确保所有未明确放行的其他请求必须经过认证才能访问。这是微服务安全的基本要求。
通过上述配置,Auth Service允许对/authenticate/signup、/authenticate/login和/authenticate/refreshtoken的请求无需认证即可通过,从而解决了“Full authentication is required”的错误。API 在转发这些请求时,Gateway也能成功收到Auth Service的响应避免了“Could not send request”的问题。
注意事项和最佳实践-
WebSecurityconfiguradapter替代方案: Spring Security5.7.0版本结束后,官方建议使用基于组件的安全配置,而不是继承websecurityconfigureradapter。虽然上述代码仍然有效,但建议使用以下函数配置,以遵循最新和最佳实践:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .csrf(csrf -> csrf.disable()) // CSRF禁用 .authorizeHttpRequests(auth -> auth .requestMatchers("/authenticate/signup", "/authenticate/login", "/authenticate/refreshtoken").permitAll() .anyRequest().authenticated() ); // 可根据需要添加JWT过滤器等 // .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class); return http.build(); } }这种方法更灵活,符合Spring Security的未来发展方向。详见Spring官方博客:Spring Security without the WebSecurityConfigurerAdapter。
-
API Gateway与服务间合作: API 作为要求的入口,Gateway起着路由和转发的作用。当Auth 在Service正确配置公共访问路径后,API Gateway可以顺利地将请求转发到这些端点。在实际生产环境中,API Gateway通常承担额外的安全责任,例如:
- 限流: 防止恶意请求或DDOS攻击。
- 日志记录: 记录进出系统的所有请求。
- 初步认证/授权: 对需要认证的请求,API Gateway可以进行初步的JWT验证,甚至可以直接处理一些授权逻辑,减轻后端服务的负担。
Spring 在Cloud微服务架构中,Spring正确配置 Security是确保系统安全和功能正常运行的关键。对于用户注册、登录等公共认证接口,请务必使用permitall()方法明确释放,以避免“Full authentication is required“错误,保证API Gateway可以顺利转发请求。同时,关注Spring 最新推荐的Security配置方法,采用函数式配置,可使代码更加现代化,易于维护,从而构建强大高效的微服务系统。
以上是Spring的解决方案 JWT认证在Cloud微服务中”Full authentication is required问题的详细内容,请多关注图灵教育的其他相关文章!