在Angular中,通过HttpInterceptor拦截器来解决此问题。以下是拦截器的代码示例:
@Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(private authService: AuthenticationService) {}
intercept(req: HttpRequestBearer ${authToken}
} });
return next.handle(req).pipe(
catchError((errorResponse: HttpErrorResponse) => {
if (errorResponse.status === 401) {
// handle unauthorized error
this.authService.logout();
}
return throwError(errorResponse);
})
);
} }
在Spring Boot中,可以通过设置相应的Session策略来解决此问题。以下是代码示例:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { http .sessionManagement() .maximumSessions(1) // 设置同时允许的最大会话数为1 .expiredSessionStrategy(new CustomExpiredSessionStrategy()) // 设置过期Session的处理策略 .sessionRegistry(sessionRegistry()); }
@Bean public SessionRegistry sessionRegistry() { return new SessionRegistryImpl(); } }
使用以上的方法,就可以确保连续的HTTP请求属于同一个会话。