Angular-SpringBoot全栈会话管理问题:连续的HTTP请求不属于同一会话。
创始人
2024-10-21 08:01:01
0

在Angular中,通过HttpInterceptor拦截器来解决此问题。以下是拦截器的代码示例:

@Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(private authService: AuthenticationService) {}

intercept(req: HttpRequest, next: HttpHandler): Observable> { const authToken = this.authService.getAuthToken(); req = req.clone({ setHeaders: { Authorization: Bearer ${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请求属于同一个会话。

相关内容

热门资讯

此事迅速冲上热搜!WePOke... 此事迅速冲上热搜!WePOker有没有透视方法(透视)一贯有辅助平台(有挂透视)-哔哩哔哩1、WeP...
透视了解!wepokerh5破... 透视了解!wepokerh5破解(透视)一贯是有辅助修改器(有挂方针)-哔哩哔哩1、首先打开wepo...
截至目前!wepoker提高好... 截至目前!wepoker提高好牌率(透视)原来是真的辅助软件(今日头条)-哔哩哔哩1、在wepoke...
透视辅助!菠萝辅助器免费版的特... 透视辅助!菠萝辅助器免费版的特点(透视)果然是有辅助app(有挂教学)-哔哩哔哩1、超多福利:超高返...
近期!佛手大菠萝辅助(透视)果... 近期!佛手大菠萝辅助(透视)果然是真的辅助工具(有挂透视)-哔哩哔哩1、每一步都需要思考,不同水平的...
目前!we poker辅助器v... 目前!we poker辅助器v3.3(透视)其实存在有辅助工具(有挂规律)-哔哩哔哩亲,关键说明,w...
近期!佛手在线大菠萝辅助(透视... 近期!佛手在线大菠萝辅助(透视)一贯是有辅助平台(有挂功能)-哔哩哔哩1、全新机制【佛手在线大菠萝辅...
透视挂透视!wepoker透视... 透视挂透视!wepoker透视脚本是什么(透视)切实是有辅助软件(了解有挂)-哔哩哔哩wepoker...
透视app!佛手在线有挂吗(透... 透视app!佛手在线有挂吗(透视)好像真的是有辅助安装(讲解有挂)-哔哩哔哩1、游戏颠覆性的策略玩法...
此事备受玩家关注!来玩app ... 此事备受玩家关注!来玩app 德州 辅助(透视)切实真的有辅助辅助器(有挂实锤)-哔哩哔哩来玩app...