我有一个在nginx后面的tomcat应用程序服务器.SSL终止于nginx.部署在tomcat上的Spring web-mvc应用程序应该在JSESSIONID上设置安全标志.如果spring有一些自动检测功能会很酷,所以我不会在开发过程中受到打扰,因为我没有SSL.
有没有办法告诉spring自动设置标志?
我使用JavaConfig来设置应用程序并使用Maven来创建可部署的war文件.
我已经检查了这个,但这看起来有些丑陋和静态: 将'secure'标志设置为JSESSION id cookie
我对打字稿有点陌生,并尝试将类型定义添加到我的所有方法中,同时尽可能精确。
当我在 Chrome 中单击时,PointerEvent
会发出 a 。在 Firefox 中,MouseEvent
会发出 a。我知道PointerEvent
扩展MouseEvent
并根据https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent#browser_compatibility Firefox 也支持这些。
MouseEvent
)github有一个很好的功能,可以显示使用gpg密钥签名git commit.
我按照以下文章:
现在我可以使用命令行默认签署我的提交和标签.
(这在github存储库中也可见/标记为"已验证")
然而,即使我打开/关闭"签名"按钮,eclipse也拒绝(正确)签署任何git提交(使用gpg).它也没有显示是否签署了提交.
我做错了什么或是eclipse/egit还没有(还)能够处理gpg?
我使用以下工具
我有一个Spring-JSON/RestAPI,它使用注释驱动的输入验证. @Valid
当我尝试验证另一个对象内的对象时,我收到以下错误.
java.lang.IllegalStateException: JSR-303 validated property 'client.application' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:153) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:108) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.validation.DataBinder.validate(DataBinder.java:866) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
...
Run Code Online (Sandbox Code Playgroud)
以下是此处使用的json数据模型:
{ // @Valid MessageDTO
"title": "Test",
"message":"Test",
"client": { // @Valid ClientDTO
"application": "Test"
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里跳过了java定义,因为它会有太多无用的噪音IMO.
我不想/不能轻易地将getter或setter添加到我的DTO中,那么我该如何关注该错误消息并将我的DataBinder配置为使用"直接字段访问"?我想为此使用JavaConfig(@Configuration
).
使用以下依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
编辑:
控制器:
@RequestMapping(path = "/send", method = { RequestMethod.POST, …
Run Code Online (Sandbox Code Playgroud) 我想使用goolge/guice根据我提供的类注释注入一个值.
AutoConfig注释
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.FIELD })
public @interface AutoConfig {
// default null not possible
Class<? extends Provider<? extends ConfigLoader<?>>> provider() default XMLAutoConfigProvider.class;
}
Run Code Online (Sandbox Code Playgroud)
这是我的注释,它允许配置应该用于带注释字段的配置类型.
用例:
@AutoConfig()
ConfigLoader<?> defaultConfig;
@AutoConfig(provider = JsonConfigProvider)
ConfigLoader<?> jsonConfig;
Run Code Online (Sandbox Code Playgroud)
我想要两个配置,一个默认/ xml一个和一个json.它们可能永远不会同时出现在同一个类中.但我不知道何时使用这一个或另一个.我在类中使用了这个方法,因为它们是由一些依赖项/库提供的,这个注释将用于一些(可插入的)子模块.
MyGuiceModule
public class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<ConfigLoader<?>>() {})
.annotatedWith(AutoConfig.class)
.toProvider(autoConfig.provider());
}
}
Run Code Online (Sandbox Code Playgroud)
这是关键部分,我无法想象如何实现它.
所以基本上我只想使用注释中指定的提供程序类.它也没有必要在这里使用提供程序类.因为autoConfig.provider().newInstance()基本上都是我需要的.(我需要在新实例上使用setter,但这就是我想在这个地方做的所有事情)
总而言之,我真正想做的是使用get(AutoConfig autoConfig)或在构造函数中将注释(或其值传递给提供者).目前我只使用构造函数来注入我想在新生成的配置实例上设置的configFile值.
我尝试将以下 json 反序列化为 java pojo。
[{
"image" : {
"url" : "http://foo.bar"
}
}, {
"image" : "" <-- This is some funky null replacement
}, {
"image" : null <-- This is the expected null value (Never happens in that API for images though)
}]
Run Code Online (Sandbox Code Playgroud)
我的 Java 类如下所示:
public class Server {
public Image image;
// lots of other attributes
}
Run Code Online (Sandbox Code Playgroud)
和
public class Image {
public String url;
// few other attributes
}
Run Code Online (Sandbox Code Playgroud)
我使用杰克逊 2.8.6
ObjectMapper.read(json, LIST_OF_SERVER_TYPE_REFERENCE);
Run Code Online (Sandbox Code Playgroud)
但我不断收到以下异常: …
我的库必须处理以任意顺序指定的多个bean(拦截器)(因为它们分布在多个配置文件中)。在应用它们之前,我必须按它们的优先级对其进行排序。我用AnnotationAwareOrderComparator.sort(beans)
那个。只要@Order
在该拦截器的类级别上添加注释,此方法就可以很好地工作。但是当我尝试在@Bean方法的@Configuration类中使用它时,它不起作用:
@Configuration
public class Config {
@Bean
@Order(1)
public ServerInterceptor exceptionTranslatingServerInterceptor() {
return ...;
}
@Bean
@Order(2)
public ServerInterceptor authenticatingServerInterceptor() {
return ...;
}
@Bean
@Order(3)
public ServerInterceptor authorizationCheckingServerInterceptor() {
return ...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加这样的测试:
@Test
void testOrderingOfTheDefaultInterceptors() {
List<ServerInterceptor> expected = new ArrayList<>();
expected.add(applicationContext.getBean(ExceptionTranslatingServerInterceptor.class));
expected.add(applicationContext.getBean(AuthenticatingServerInterceptor.class));
expected.add(applicationContext.getBean(AuthorizationCheckingServerInterceptor.class));
List<ServerInterceptor> actual = new ArrayList<>(this.registry.getServerInterceptors());
assertEquals(expected, actual); // Accidentally passes
// System.out.println(actual);
Collections.shuffle(actual);
AnnotationAwareOrderComparator.sort(actual);
assertEquals(expected, actual); // Fails
// System.out.println(actual);
}
Run Code Online (Sandbox Code Playgroud)
然后测试将失败。通过调试,我知道AnnotationAwareOrderComparator.findOrder(Object)
对于这些bean的顺序总是返回null(未指定)。可能是因为未实例化Bean实例,因此在类级别上既未实现订单也未具有订单注释。我必须启用BeanPostProcessor或配置选项吗?
如何告诉spring保留带注释的顺序或使用应用程序上下文的bean定义对bean进行适当排序?
我正在使用Java 8,Tomcat 8,Spring-WebMVC 4.2.2.RELEASE,FasterXML 2.6.3.
我的控制器中有以下方法
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public void updateCurrentUserDetails(@RequestBody final UserDTO userDTO) {
final UserWithId user = SecurityUtil.getCurrentUser();
this.userAccountService.updateUserDetails(user.getUserId(), user.getUsername(), userDTO);
}
Run Code Online (Sandbox Code Playgroud)
此方法返回void,该值在空(0字节)响应中解析.但是,连接到服务器的客户端总是希望JSON响应,如果它是空响应.
所以我想配置Spring/Jackson在这种情况下返回{}(2字节).
我已经考虑过在返回无效的调用中返回新的Object(),但IMO这是一个肮脏的问题,必须有更好的东西.
Spring允许在@ExceptionHandlers
内部定义@RestControllerAdvice
。
我已经ExceptionHandlers
在那里为HTTP 400、404、405 等定义了许多其他协议。但是,HTTP 406的ExceptionHandler(NOT_ACCEPTABLE)似乎不起作用。处理程序被触发,我在日志中检查了该结果,但未使用结果。
我的目标是返回带有JSON正文的HTTP 406。
变体1
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public ErrorDTO requestMethodNotSupported(final HttpMediaTypeNotAcceptableException e) {
final ErrorDTO dto = new ErrorDTO(HttpStatus.NOT_ACCEPTABLE, "http.media_not_acceptable");
return dto;
}
Run Code Online (Sandbox Code Playgroud)
变体2
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public ResponseEntity<ErrorDTO> requestMethodNotSupported2(final HttpMediaTypeNotAcceptableException e) {
final ErrorDTO dto = new ErrorDTO(HttpStatus.NOT_ACCEPTABLE, "http.media_not_acceptable");
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).contentType(MediaType.APPLICATION_JSON_UTF8).body(dto);
}
Run Code Online (Sandbox Code Playgroud)
但是我总是从Tomcat收到类似于以下内容的HTML响应:
HTTP状态406-
类型:状态报告
信息:
描述:此请求标识的资源只能根据请求“接受”标头生成特性不可接受的响应。
代替
{“ errorCode”:406,“ errorMessage”:“ http.media_not_acceptable”}
请求标头:
实际响应标题:
预期响应标题:
我知道我可以简单地“修复”客户端发送的Accept-Header,但是如果服务器不知道如何响应,则服务器应始终以JSON响应。
我使用Spring 4.3.3.RELEASE和Jackson 2.8.4。
我尝试在我的一个脚本中获得最大性能,而不进行重大重构。
我发现了使用反射从 Field 创建 BiConsumer 的方法。
return (c, v) -> {
try {
field.set(c, v);
} catch (final Throwable e) {
throw new RuntimeException("Could not set field: " + field, e);
}
};
Run Code Online (Sandbox Code Playgroud)
反射有缓慢的名声。所以我想我可以使用方法句柄。
Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.unreflectSetter(field);
return (c, v) -> {
try {
mh.invoke(c, v);
} catch (final Throwable e) {
throw new RuntimeException("Could not set field: " + field, e);
}
};
Run Code Online (Sandbox Code Playgroud)
这已经快了一点点。然而BiConsumer
,它FunctionalInterface
可以以某种方式生成。
public static <C, V> BiConsumer<C, V> …
Run Code Online (Sandbox Code Playgroud) java ×8
spring ×4
rest ×2
spring-mvc ×2
annotations ×1
cookies ×1
eclipse ×1
firefox ×1
git ×1
gnupg ×1
guice ×1
http ×1
inject ×1
jackson ×1
json ×1
maven ×1
methodhandle ×1
mouseevent ×1
nginx ×1
reflection ×1
sorting ×1
typescript ×1
validation ×1