Json Spring Security 4.0内容协商限制
我有一个使用SpringSecurity4.0的应用程序,我现在担心这个应用程序可能在RESTWeb服务上发送的内容协商响应,即。 我的目标是在与请求类型无关的全局基础上限制响应,即,如果是通过MVC或某种websocket的REST http get请求(尽管我不确定这是否适用于websocket),则响应应仅作为json而不是XML返回。我不想支持xml或任何协商格式 我之所以担心这件事是因为我看了 一个名为Mike Wiesner的绅士在infoq上制作的关于spring应用程序安全隐患的视频 我知道我可以在这种情况下使用注释@RequestMapping和子选项“products”,例如Json Spring Security 4.0内容协商限制,json,spring-security,content-negotiation,Json,Spring Security,Content Negotiation,我有一个使用SpringSecurity4.0的应用程序,我现在担心这个应用程序可能在RESTWeb服务上发送的内容协商响应,即。 我的目标是在与请求类型无关的全局基础上限制响应,即,如果是通过MVC或某种websocket的REST http get请求(尽管我不确定这是否适用于websocket),则响应应仅作为json而不是XML返回。我不想支持xml或任何协商格式 我之所以担心这件事是因为我看了 一个名为Mike Wiesner的绅士在infoq上制作的关于spring应用程序安全隐患的
@RequestMapping(produces={MediaType.APPLICATION_JSON_VALUE} , value = "/target/get", method=RequestMethod.GET)
但由于我有这么多的控制器,我将是一个噩梦,把额外的子选项对所有的人
我知道还有其他注释,比如
@XmlTransient
@JsonIgnore
这可以帮助我完成我想做的事情,即在内容协商发生变化的情况下,使一些fild(getter/setter)不被公开,而是将这些注释放在每个fild上
getter/setter将是更大的问题
因此,我的问题是如何在全球范围内做到这一点。我想这应该在扩展WebMVCConfigureAdapter的MVCConfig类中完成?
我的意思是覆盖configureContentNegotiation方法,有多个示例可以这样做,但这些示例仅说明如何设置默认行为。我的问题是我们如何限制行为,即如果http请求带有“Accept”头应用程序/xml,我如何在全局基础上拒绝它
默认行为的示例:
所以我所做的是
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).
If anything else then a json comms into the http request
reject this request or smply ignore it on a global basis.
Do not send/support xml, xhtml, html etc.
}
}
在过去的几天里,我碰巧正在研究一个与这个问题相关的问题。我们在代码库中手动配置一个
ContentNegotiationManager
,在此过程中,我们通过提供一个覆盖的HeaderContentNegotiationStrategy
来限制Spring PPA策略中基于头的部分,该策略通过接受类似于您想要的头进行限制。我快速查看了ContentNegotiationConfigurer(我从未使用过),它似乎没有提供一个选项来更改HeaderContentNegotiationStrategy
的映射,因此下面是我们设置ContentNegotiationManager
的代码片段
@Bean
public ContentNegotiationManager contentNegotiationManager() {
//Supply a Map<String, org.springframework.http.MediaType>
PathExtensionContentNegotiationStrategy pathBased = new PathExtensionContentNegotiationStrategy(supportedMediaTypes());
//Supply a Map<org.springframework.http.MediaType, org.springframework.http.MediaType>
HeaderContentNegotiationStrategy headerBased = new MappingHeaderContentNegotiationStrategy(contentTypeMap());
FixedContentNegotiationStrategy defaultStrategy = new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON);
return ContentNegotiationManager(pathBased, headerBased, defaultStrategy);
return retval;
}
@Bean
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setRemoveSemicolonContent(false);
handlerMapping.getFileExtensions().add("json");
handlerMapping.setUseRegisteredSuffixPatternMatch(true);
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager());
return handlerMapping;
}