Spring安全:如何使用标志强制https?
使用Spring security,我使用Spring安全:如何使用标志强制https?,https,spring-security,Https,Spring Security,使用Spring security,我使用security:intercept urltag配置需要https保护的web应用程序部分 例如: 这样,文件名中包含“secure”的所有请求或最低级别包含“secure”的目录都将获得https,其余请求将获得http 有没有一种方法可以使用标志使Spring force https生效? 我无法得到任何需要在url的“?”之后进行处理的东西 http://domain/abc?secure=true=>https http://domain
security:intercept url
tag配置需要https保护的web应用程序部分
例如:
这样,文件名中包含“secure”的所有请求或最低级别包含“secure”的目录都将获得https,其余请求将获得http
有没有一种方法可以使用标志使Spring force https生效?
我无法得到任何需要在url的“?”之后进行处理的东西
=>httpshttp://domain/abc?secure=true
http://domain/abc =>http
http://domain/abc?secure=true
至https://domain/abc?secure=true
有没有办法做到这一点?
本例中的第一个模式实现了在“?”之前出现“安全”的结果,但我正在寻找一个解决方案/模式,以便在“?”之后只显示一个标志。据我所知,您希望有一个URL参数,告诉spring将您重定向到https频道 你有没有试过:
<security:intercept-url pattern="/**/*?secure*" requires-channel="https"/>
或者如果这不起作用,尝试一个转义“?”:
<security:intercept-url pattern="/**/*\?secure*" requires-channel="https"/>
我刚刚查看了
FilterChainProxy
(在3.0.5.RELEASE中)的源代码,它使用正则表达式匹配模式。因此,您可以看看:
org.springframework.security.web.util.RegeSurlPathMatcher#pathMatchesUrl(对象编译路径,字符串url)
或
org.springframework.security.web.FilterChainProxy#getFilters(字符串url)
看看你到底需要什么样的图案
公共类BeanPostProcessor MPL实现BeanPostProcessor
{
最终静态专用记录器log=Logger.getLogger(BeanPostProcessorImpl.class);
@凌驾
公共对象后处理BeforeInitialization(对象bean、字符串beanName)抛出BeanException
{
返回豆;
}
@凌驾
公共对象后处理初始化后(对象bean、字符串beanName)抛出BeansException
{
if(DefaultFilterInocationSecurityMetaDataSource的bean实例)
{
log.info(“*******后处理”+beanName);
((DefaultFilterInvocationSecurityMetadataSource)bean);
}
返回豆;
}
}
这种方式
终于奏效了。我不明白这个问题。你是说
http://domain/abc?secure=true
应将用户重定向到https://domain/abc
?或者您的意思是“任何包含请求参数secure=true
的URL都必须使用HTTPS”。是的,当您查看提到的getFilters方法时,您会看到“?”后面的所有内容都将被剥离,无论您使用的是哪种过滤器。解决方法是将StripQueryStringfromURL设置为false。感谢您的指针。您可以通过spring上下文设置SetTripQueryStringFromUrls(false),而不是后处理器。你不需要这样编码,我也不喜欢。我没有找到通过上下文设置属性的方法。在DefaultFilterInvocationSecurityMetadataSource或FilterChainProxy上创建bean定义会产生各种可怕的错误。使用BeanPostProcessor是我发现的唯一为spring创建的bean设置属性的方法。嗯,好吧,然后,您可以只扩展FilterChainProxy覆盖getStripQueryStringFromUrls()以始终返回false,并在web.xml中使用您的FilterChainProxy,而不是默认值。我不确定这样是否更好。。。