小编Aym*_*ani的帖子

由 aws API 制作的 HttpRequest 拦截器

我正在开发一个项目,该项目使用 cognito 作为身份验证服务来保护使用 nodeJS 制作的无服务器休息 API。我已成功关闭未经身份验证的客户端的 API。现在,每当我从 Angular 客户端发出请求时,我都需要在标头中自动注入一个令牌。我尝试的是实现这样的 HttpInterceptor :

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


    req = req.clone({
        setHeaders: {
          'Content-Type' : 'application/json; charset=utf-8',
          'Accept'       : 'application/json',
              'Authorization': `${this.authService.userToken}`,
              //#endregion
          },
          
        })
  

  return next.handle(req);
}
Run Code Online (Sandbox Code Playgroud)

当使用标准的 Angular HttpClient 发出请求时,这对我来说总是很有效。但现在,当我使用 aws-amplify 包中的 API 向我的 API.Gateway 发出请求时,这些请求无法像这样被拦截。这是我提出请求的方式:

import { API } from 'aws-amplify';
.
.
.
 return API.get('apiName', '/users',{})
Run Code Online (Sandbox Code Playgroud)

而且这些都没有使用 Angular HttpClient。

编辑:也在 app.module.ts 中:

providers : [{
    provide : HTTP_INTERCEPTORS,
    useClass: HttpRequestInterceptor,  --> My interceptor class
    multi   : true, …
Run Code Online (Sandbox Code Playgroud)

authentication api amazon-web-services aws-api-gateway angular

6
推荐指数
1
解决办法
2137
查看次数

Spring Security + JWT 403禁止错误

因此,我正在尝试使用 SpringSecurity 和 JWT 令牌来保护 api。我可以获得令牌,但每次我尝试使用令牌访问受保护的端点时,都会收到 403 Forbidden。我有一个包含角色和用户的数据库。这是我的春季安全配置:

 httpSecurity.csrf().disable().cors().disable()
            // dont authenticate this particular request
            .authorizeRequests().antMatchers("/authenticate", "/register").permitAll()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/system/**").hasRole("ADMIN")
            //.antMatchers("/system").permitAll()
            // all other requests need to be authenticated
                    //.anyRequest().permitAll()
            .and().
            // make sure we use stateless session; session won't be used to
            // store user's state.
                    exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    // Add a filter to validate the tokens with every request
    httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
Run Code Online (Sandbox Code Playgroud)

我尝试调试,可以解码令牌,可以访问用户信息,并且可以获得包含角色的用户对象。这就是为什么我真的不知道发生了什么。

调试变量:

这是我的 RequestFilter 类的过滤方法:

String username = null;
String jwtToken = null;
// JWT Token is in the form …
Run Code Online (Sandbox Code Playgroud)

security spring jwt

4
推荐指数
1
解决办法
7909
查看次数