我使用Java EE 5.我为所有EJB编写了一个拦截器,它有三种记录方法:
public class DefaultInterceptor {
public static final String PREFIX = "!!!!!!!!!Interceptor:";
@PostConstruct
public void postConstruct(InvocationContext ctx) {
try {
System.out.println(PREFIX + " postConstruct");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@PreDestroy
public void preDestroy(InvocationContext ctx) {
try {
System.out.println(PREFIX + " predestroy");
System.out.println(PREFIX + "ctx.preceed=" + ctx.proceed());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
System.out.println(PREFIX + "method invocation '" + ctx.getMethod().getName() + "'"); …Run Code Online (Sandbox Code Playgroud) 我正在使用一个简单的Spring Interceptor类来记录我的Android应用程序中RestTemplate对象的所有REST请求/响应.到目前为止一切正常.
public class LoggerInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
Log.d(TAG, "Request body: " + new String(body));
// ...more log statements...
ClientHttpResponse response = execution.execute(request, body);
Log.d(TAG, "Response Headers: " + response.getHeaders());
return response;
}
Run Code Online (Sandbox Code Playgroud)
在初始化时我打电话:
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
LoggerInterceptor loggerInterceptor = new LoggerInterceptor();
interceptors.add(loggerInterceptor);
restTemplate.setInterceptors(interceptors);
Run Code Online (Sandbox Code Playgroud)
但是我无法登录response.getBody()上面的方法,因为InputStream被消耗一次并在以后再次使用时抛出IllegalStateException.有没有解决方法,以便我也可以记录响应正文?
我正在编写REST API,使用RestEasy 2.3.4.Final.我知道拦截器将拦截我的所有请求,并且PreProcessInterceptor将是第一个(在所有事情之前)被调用.我想知道如何在调用特定方法时调用此Interceptor.
我试图使用PreProcessInterceptor和AcceptedByMethod,但我无法读取我需要的参数.例如,我只需要在调用此方法时运行我的Interceptor:
@GET
@Produces("application/json;charset=UTF8")
@Interceptors(MyInterceptor.class)
public List<City> listByName(@QueryParam("name") String name) {...}
Run Code Online (Sandbox Code Playgroud)
更具体地说,我需要在所有具有a的方法中运行我的拦截器 @QueryParam("name")
在它的签名上,这样我就可以抓住名字并在所有事情之前做点什么.
可能吗?我试图在Interceptor中捕获"name"参数,但我无法做到这一点.
有人可以帮帮我吗?
我有一个实现两个拦截器的Spring 3 Web App.我使用注释@Configuration的配置类.代码如下:
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
super.addInterceptors(registry);
registry.addInterceptor(homeInterceptor()).addPathPatterns("/");
registry.addInterceptor(allInterceptor());
}
Run Code Online (Sandbox Code Playgroud)
无论我将拦截器添加到注册表的顺序是什么,allInterceptor的preHandle函数总是在homeInterceptor的preHandle之前调用.有谁知道如何控制拦截器被调用的顺序?
谢谢!
使用AngularJS拦截器,是否可以将我的应用程序调用区分为$ http(直接通过$ resource)与Angular本身针对静态资源(如视图)发出的请求,而不检查URL?
我在HTTP拦截器中添加自定义授权标头,如下所示:
transparentAuthServices.factory('authHttpInterceptor',
function (localSessionStorage) {
return {
'request': function (config) {
if (!config.ignoreAuthInterceptor && localSessionStorage.hasSession()) {
var sessionId = localSessionStorage.getSession().sessionId;
config.headers['Authorization'] = 'ARTAuth sessionId="' + sessionId + '"';
return config;
} else {
return config;
}
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我不需要授权静态资源,我的服务器不检查它们.我可以查看网址并跳过那些以'/ app'开头的(在我的情况下),但我想知道是否有一个优雅的解决方案?
这纯粹是一个学习实验(耶和华科学!).这并不意味着实际上可以在任何地方使用.我想了解EF6的命令树拦截器是如何工作的.
我正在尝试修改截获的命令树,为所有查询添加"IsActive = 1"过滤器.我注意到这类文件的文档存在严重缺陷.两个问题:
如何有选择地拦截命令树,例如实现接口的实体(例如IHasAnActiveProperty)?现在我注意到拦截器正在拦截对历史上下文的查询,这与我的无关MyEntity.
如何将我的过滤器添加到所有查询?
public class MyEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<MyEntity> Entities { get; set; }
public MyDbContext() : base("default") { }
public MyDbContext(string nameOrConnectionString)
: base(nameOrConnectionString) {}
}
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
AddInterceptor(new MyInterceptor());
}
}
public class MyInterceptor : …Run Code Online (Sandbox Code Playgroud) 我刚刚将我的Angular v4.2.5应用升级为Angular v4.3.6主要使用Interceptors.这是一个非常棒的功能,并提供了一种拦截HTTP调用的简洁方法.
但是,我似乎无法在模块级别范围内设置拦截器.例如,我有一个AppModule,还有两个模块AModule和BModule.双方AModule并BModule越来越列入AppModule.
现在,在Angular 4中是否有一种方法,我可以在模块级别范围拦截器,这样我用于HTTP请求的拦截器AModule就不应该与HTTP请求一起使用BModule.目前,拦截器正在所有HTTP调用中共享,这不是我预期的.我知道providers所有模块都会合并到父模块中,但是有什么方法可以限制这样的事情吗?
任何帮助都非常感谢.
我在我的应用程序中使用 Entity Framework Core 2。我的数据库中有很多可为空的字符串列。
问题是我想将空字符串保存NULL在数据库中。
在旧版本的 EF 中,我使用 aIDbCommandInterceptor来实现拦截器,但在 EF Core 2 中,我不知道如何编写一个?
我有一个 grpc-java 服务器,需要在处理请求之前对身份验证服务进行异步调用。我认为这应该在拦截器中完成,但它需要从interceptCall()同步返回一个Listener
class AuthInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next
) {
String token = ""; //get token from headers
authService.authorize(token).subscribe(
ok -> // process the request
error -> call.close(Status.UNAUTHENTICATED, headers)
);
// Here we need to return a Listener, but we haven't started a call yet
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是:如何从 ServerInterceptor 进行异步调用,如果无法完成,那么异步验证 grpc 请求的正确方法是什么?我知道它可以直接在 grpc 服务中使用 StreamObservers 完成,但请求授权是一个交叉问题,拦截器似乎是一个完美的地方。
我正在尝试创建一个用于在 javascript 中获取的拦截器(更具体地说是 React)。它应该从每次调用的 fetch 中获取结果,如果是 401 错误,则应该向另一个路由发起新的 fetch 调用以获取 cookie(刷新令牌)。然后,应再次尝试原始的提取调用(因为现在用户已登录)。
我已经成功触发新的 fetch 调用并发回每个调用的 cookie,但是我遇到了以下两个问题:
我现在不知道如何在收到刷新令牌后重试 fetch 调用。那可能吗?我找到了 fetch-retry npm ( https://www.npmjs.com/package/fetch-retry ),但不确定如何以及是否可以在拦截器上实现它,当它应该为原始的 fetch 调用完成时。
我似乎在异步等待方面做错了(我认为),因为拦截在返回数据之前没有等待提取调用(原始提取上的状态代码似乎是 401 而不是 200,在我们得到之后它应该是cookie。我还尝试返回拦截器内获取的响应,但返回未定义)。
知道如何解决这个问题吗?有谁做过类似的事情吗?
下面是我的代码:
(function () {
const originalFetch = fetch;
fetch = function() {
return originalFetch.apply(this, arguments).then(function(data) {
if(data.status === 401) {
console.log('not authorized, trying to get refresh cookie..')
const fetchIt = async () => {
let response = await fetch(`/api/token`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
}); …Run Code Online (Sandbox Code Playgroud) interceptor ×10
java ×4
javascript ×3
spring ×2
android ×1
angular ×1
angularjs ×1
asynchronous ×1
c# ×1
ejb-3.0 ×1
grpc ×1
http ×1
inputstream ×1
module ×1
rest ×1
resteasy ×1
spring-mvc ×1