标签: interceptor

Spring mvc 3:如何在拦截器中获取路径变量?

在Spring MVC控制器中,我可以使用@PathVariable获取路径变量,以获取@RequestMapping中定义的变量的值.如何在拦截器中获取变量的值?

非常感谢你!

spring path-variables interceptor

22
推荐指数
3
解决办法
2万
查看次数

我需要两个AngularJS $ http服务实例或者什么?

我想为我的$ http服务添加一个响应拦截器,以便进行错误处理.拦截器逻辑包括在必要的情况下使用$ http向服务器发送错误消息,我不希望向服务器发送有关错误消息的错误消息,我的意思是,我希望在向服务器发送错误消息时禁用我的拦截器.

我的想法是创建一个名为'remote_log'的服务,并将所有向服务器发送错误所需的代码放入其中.该服务当然将使用$ http服务并将其置于其依赖列表中.

然后将拦截器的依赖关系添加到'remote_log'服务,并在需要向服务器发送错误时使用拦截器内的'remote_log'.问题是:

当$ http服务仍未实例化/可访问时,必须使用$ httpProvider定义拦截器,因此,拦截器代码内部不能依赖于$ http服务,因为发生了"循环依赖"错误.

我认为我唯一的选择是在我的'remote_log'中创建一个单独的$ http服务实例,这个实例不使用我在创建拦截器时设置的$ httpProvider配置.我的问题是:我怎么能这样做?还有其他想法吗?

error-handling service http interceptor angularjs

22
推荐指数
1
解决办法
7753
查看次数

Interceptor Angular 4.3 - 在克隆的请求上设置多个标头

我刚刚注意到新Interceptor中不再支持可能在先前的HTTP RequestsOption中使用的Header对象.

这是新的拦截器逻辑:

// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
Run Code Online (Sandbox Code Playgroud)

我现在有两种方法可以在此请求中添加标题:

例:

headers?: HttpHeaders;

    headers: req.headers.set('token1', 'asd')
Run Code Online (Sandbox Code Playgroud)
setHeaders?: {
   [name: string]: string | string[];
};

    setHeaders: {
             'token1': 'asd',
             'token2': 'lol'
    }
Run Code Online (Sandbox Code Playgroud)

如何在此请求中有条件地添加多个标头?与我以前使用Header对象相同:

 myLovellyHeaders(headers: Headers) {
    headers.set('token1', 'asd');
    headers.set('token2', 'lol');
     if (localStorage.getItem('token1')) {
     headers.set('token3', 'gosh');
     }
    }
    const headers = new Headers();
    this.myLovellyHeaders(headers);
Run Code Online (Sandbox Code Playgroud)

header interceptor angular

22
推荐指数
4
解决办法
3万
查看次数

使用Angular拦截Unathorized API调用

我试图拦截401403错误刷新用户令牌,但我不能让它运作良好.我所取得的就是这个拦截器:

app.config(function ($httpProvider) {

  $httpProvider.interceptors.push(function ($q, $injector) {

    return {
      // On request success
      request: function (config) {
        var deferred = $q.defer();

        if ((config.url.indexOf('API URL') !== -1)) {
          // If any API resource call, get the token firstly
          $injector.get('AuthenticationFactory').getToken().then(function (token) {
            config.headers.Authorization = token;

            deferred.resolve(config);
          });
        } else {
          deferred.resolve(config);
        }

        return deferred.promise;
      },

      response: function (response) {
        // Return the promise response.
        return response || $q.when(response);
      },

      responseError: function (response) {
        // Access token invalid …
Run Code Online (Sandbox Code Playgroud)

javascript interceptor angularjs angular-promise

21
推荐指数
1
解决办法
2233
查看次数

如何将参数传递给HttpInterceptor?

我正在使用Angular 4.3.1和HttpClient.有一个HttpInterceptor来设置一些标题.

在一些http get请求中,我需要设置一个不同的头.无论如何我可以为这个特定的HttpRequest传递一些param给这个HttpInterceptor?

@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if(request.custom.param1) // how can i do this 
      request = request.clone({
        setHeaders: {
          'header1': 'xxxxxx'
          }
      });

    else
      request = request.clone({
        setHeaders: {
          'header2': 'yyyyyy'
          }
      });


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

interceptor typescript angular

21
推荐指数
3
解决办法
8903
查看次数

Spring MVC中的异常处理程序

我想创建一个异常处理程序,它将拦截我项目中的所有控制器.这可能吗?看起来我必须在每个控制器中放置一个处理程序方法.谢谢你的帮助.我有一个发送Json响应的弹簧控制器.因此,如果发生异常,我想发送一个可以从一个地方控制的错误响应.

spring spring-mvc custom-exceptions interceptor

20
推荐指数
3
解决办法
2万
查看次数

使用vuex的vue 2 JS中的Axios拦截器

我在vuex商店成功登录之后存储令牌,如下所示:

axios.post('/api/auth/doLogin.php', params, axiosConfig)
    .then(res => {
        console.log(res.data); // token
        this.$store.commit('login', res.data);
    })
Run Code Online (Sandbox Code Playgroud)

axiosConfig是我只设置baseURL的文件,export default { baseURL: 'http://localhost/obiezaca/v2' }params只是发送到后端的数据.

我的vuex文件看起来是:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        logged: false,
        token: ''
    },
    mutations: {
        login: (state, response) => {
            state.logged = true;
            state.token = response;
            console.log('state updated');
            console.log('state.logged flag is: '+state.logged);
            console.log('state.token: '+state.token);
        },
        logout: (state) => {
            state.logged = false;
            state.token = '';
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

它工作正常,我可以根据 …

interceptor vue.js axios vuex vuejs2

20
推荐指数
1
解决办法
2万
查看次数

如何使用angular2 http API跟踪上传/下载进度

有许多adhoc库支持angular2的上传/下载进度,我不知道如何使用native angular2 http api在上传/下载时显示进度.

我想使用原生http api的原因是因为我想利用它

  1. 本机http api周围的http拦截器(http API包装器),用于验证,缓存和丰富正在发送的http请求,例如this&this
  2. 除了angular之外,http api比任何adhoc API都强大得多

一篇关于如何使用angular的http api进行上传/下载的好文章

但该文章提到,没有本土方式来支持进步.

有没有人尝试使用http api来显示进度?

如果没有,你知道角度回购中的问题吗?

http interceptor angular2-http angular

19
推荐指数
1
解决办法
2万
查看次数

如何在OkHttp响应中更改正文?

我正在使用改造.要捕获响应,我正在使用Interceptor:

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(myinterceptor);
Run Code Online (Sandbox Code Playgroud)

这是拦截器的代码:

new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        if (path.equals("/user")){
            String stringJson = response.body().string();
            JSONObject jsonObject = new JSONObject(stringJson);
            jsonObject.put("key",1);
            //here I need to set this new json to response and then return this response
Run Code Online (Sandbox Code Playgroud)

如何在OkHttp响应中更改正文?

java android interceptor retrofit okhttp

18
推荐指数
3
解决办法
2万
查看次数

处理 Kotlin 协程中自定义 okhttp 拦截器抛出的异常

Interceptor在我的 Android 应用程序中使用自定义和 Retrofit 客户端,在某些特定情况下会引发异常。我正在尝试使用 Kotlin 协程使其工作。

问题是我无法处理前面提到的错误,因为在从 Interceptor 实例中抛出异常的那一刻,它会使整个应用程序崩溃,而不是在协程的try/catch语句中被捕获。当我使用 Rx 实现时,异常完美地传播到onError回调中,在那里我能够以我需要的方式处理它。

我想这与用于网络调用的底层线程有某种关系,请从调用的地方、在抛出异常之前的拦截器和堆栈跟踪中查看下面的日志:

2019-11-04 17:17:34.515 29549-29729/com.app W/TAG: Running thread: DefaultDispatcher-worker-1
2019-11-04 17:17:45.911 29549-29834/com.app W/TAG: Interceptor thread: OkHttp https://some.endpoint.com/...

2019-11-04 17:17:45.917 29549-29834/com.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.app, PID: 29549
    com.app.IllegalStateException: Passed refresh token can\'t be used for refreshing the token.
        at com.app.net.AuthInterceptor.intercept(AuthInterceptor.kt:33)
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能正确地从拦截器中捕获和处理这个异常?我错过了什么吗?

android exception interceptor okhttp kotlin-coroutines

18
推荐指数
2
解决办法
6203
查看次数