我真的很欣赏Spring 3 anoation驱动的Web控制器映射
我有很多带有签名的控制器:
@RequestMapping(value = "solicitation/create",method = RequestMethod.POST)
public String handleSubmitForm(Model model, @ModelAttribute("solicitation") Solicitation solicitation, BindingResult result)
Run Code Online (Sandbox Code Playgroud)
但我的问题是,我想编写一个拦截器,它会在处理后通过BindingResults - 我如何从HttpRequest或HttpResponse获取它们?
因为intercpetor方法具有相似的签名
public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
Run Code Online (Sandbox Code Playgroud) 你能解释一下这段代码的作用:
App.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push('HttpSpinnerInterceptor');
$httpProvider.defaults.transformRequest.push(function (data, headersGetter) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.gif') %>");
return data;
});
}]);
App.factory('HttpSpinnerInterceptor', function ($q, $window) {
return function (promise) {
return promise.then(function (response) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.png') %>");
return response;
}, function (response) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.png') %>");
return $q.reject(response);
});
};
});
Run Code Online (Sandbox Code Playgroud)
我完全没有理解,除了一些猜测,它拦截了一些响应并注入了图像的src属性.
我不明白HttpSpinnerInterceptor是如何以及何时被调用的以及"promise"参数是什么.
我知道两个拦截器都用于防止重复提交表单吗?但两者之间究竟有什么区别?哪一个比其他人有额外优势?
我正在尝试使用tokenStruts附带的拦截器来实现CSRF检查.但是,而不是使用<form />我从一些JS内部进行AJAX调用:
foo.jsp:
<!DOCTYPE html>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<s:token />
<script>
var strutsToken = "<s:property value="#session['struts.tokens.token']" />";
</script>
<script src="bar.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
bar.js:
$.ajax({
url: '/endpoint',
data: strutsToken,
dataType: 'jsonp',
cache: true,
success: function() { console.log('success'); },
error: function() { console.log('failure'); }
});
Run Code Online (Sandbox Code Playgroud)
我已经确认令牌值正在变成JS变量:
> strutsToken
"N3ZLLLR2Y3QGMZP0L3UCYWI5CO5NYZEY"
Run Code Online (Sandbox Code Playgroud)
不幸的是,当发出AJAX请求时,invalid-token服务器上会抛出错误.
我试图做的是什么,如果是的话,怎么样?
我在angularjs spa中有以下工厂定义:
(function () {
'use strict';
angular.module('snApp')
.factory('Auth', ['$http', 'localStorageService', function ($http, localStorageService) {
//code goes here
}]);
})();
Run Code Online (Sandbox Code Playgroud)
然后在我的应用程序配置中,我有以下http拦截器设置:
var app = angular.module('snApp', ['ui.router', 'LocalStorageModule', ])
.config(['$logProvider', '$stateProvider', '$urlRouterProvider', '$httpProvider', '$provide', function ($logProvider, $stateProvider, $urlRouterProvider, $httpProvider, $provide) {
// Intercept http calls.
$provide.factory('RequestHttpInterceptor', function ($q, Auth) {
return {
// On request success
request: function ($config) {
if (Auth.user) {
$config.headers['XToken'] = Auth.user.token;
}
return $config;
}
};
});
// Add the interceptor to the $httpProvider.
$httpProvider.interceptors.push('RequestHttpInterceptor');
}]); …Run Code Online (Sandbox Code Playgroud) 我正在尝试拦截并将所有SQL命令记录到数据库中.我也需要参数及其值.但command.Parameters没有IEnumerable.我可以去找一个for-statement但有点感觉就像一个退缩.
public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
_stopwatch.Stop();
if (interceptionContext.Exception != null)
{
_logger.Error(interceptionContext.Exception, "Error executing command: {0}", command.CommandText);
string param = string.Empty;
if (command.Parameters.Count > 0)
{
foreach (var t in command.Parameters)
{
param += t....
}
}
Log log = new Log()
{
Date = DateTime.UtcNow,
LogLevel = LogLevel.Error,
Message = command.CommandText + "\r\n " + param.
};
}
else
_logger.TraceApi("SQL Database", "CommandInterceptor.ScalarExecuted", _stopwatch.Elapsed, "Command: {0}: ", command.CommandText); …Run Code Online (Sandbox Code Playgroud) 我在JEE应用程序中有一些SessionBeans,并且喜欢使用BeanValidation来验证我的参数.因此整个验证过程是通用的,并封装在一个地方.
我看到的唯一缺点是客户端得到EJBException并且必须打开主要的Exception.还有一些我看不到的问题或脏东西,甚至是更好的方法吗?
我正在实现一个HandlerInterceptor需要在处理多个路径的请求之前/之后执行业务逻辑.我想通过模拟请求句柄生命周期来测试Interceptor的执行.
以下是拦截器的注册方式:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/test*"/>
<bean class="x.y.z.TestInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
我不仅要测试preHandle和postHandle方法,还要测试到路径的映射.
我需要阻止CXF记录MultipartBody对象中的附件的二进制数据(由AbstractLoggingInterceptor出站消息中的抛出).当我添加my时LoggingInInterceptor,我设置setShowBinaryData为false,但这似乎不会阻止多部分消息中的二进制数据被记录.
我不确定是否需要创建自定义loggingInInterceptor,或者是否有一种配置现有拦截器的方法来截断它找到的任何二进制数据.停止它完全记录MultipartBody响应,或截断数据都是可接受的解决方案.
我在cxf-spring-boot-starter-jaxws版本3.2.7 的插件的帮助下将Apache CXF与Spring Boot一起使用。
我的意图是自定义LoggingInterceptors,但是当我创建以下类时:
public class CustomLoggingInInterceptor extends org.apache.cxf.interceptor.LoggingInInterceptor {}
Run Code Online (Sandbox Code Playgroud)
但是我的IDE删除了LoggingInInterceptor并抱怨说它已被弃用
使用日志模块rt / features / logging代替
那么如何使用该模块自定义日志记录拦截器呢?
interceptor ×10
java ×4
logging ×3
angularjs ×2
cxf ×2
spring-mvc ×2
struts2 ×2
ajax ×1
c# ×1
httpresponse ×1
java-ee ×1
javascript ×1
junit ×1
mapping ×1
promise ×1
spring-test ×1
sqlcommand ×1
token ×1