我正在使用带有JSP的Spring Security 3.0.我创建了一个RequireVerificationFilter,可以将未经验证的用户重定向到"验证您的电子邮件"页面.
我在最后一个地方将过滤器添加到spring安全过滤器堆栈中,如下所示:
我的app-config.xml中的Bean定义:
<bean id="requireVerificationFilter" class="com.ebisent.web.RequireVerificationFilter" />
Run Code Online (Sandbox Code Playgroud)
过滤器添加到我的security-config.xml中的spring安全过滤器列表中:
<custom-filter ref="requireVerificationFilter" after="LAST" />
Run Code Online (Sandbox Code Playgroud)
过滤器有效,但它会过滤自己的重定向网址.也就是说,过滤器会将未经验证的用户重定向到/ access/verify,但过滤器也会捕获该URL,过滤器会无限制地尝试重定向.
我尝试使用<filter-mapping>标记来限制这个新过滤器适用的URL,但这似乎不像我想象的那样工作.这是我添加的web.xml条目:
<filter>
<filter-name>requireVerificationFilter</filter-name>
<filter-class>com.ebisent.web.RequireVerificationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requireVerificationFilter</filter-name>
<url-pattern>/account/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
我在春季安全文档中阅读了"添加自己的过滤器",但没有找到答案.
我的问题是,如何指定我的过滤器适用的URL?
更新:
我通过在过滤器本身中指定允许的URL来实现此功能.这对我来说很好,但如果有更好/更"弹性"的方式,我会很高兴听到它.
在用户使用Spring Security初次登录时,实现强制密码更改的最优雅方法是什么?
我尝试实现这里AuthenticationSuccessHandler提到的自定义,但正如rodrigoap所提到的,如果用户在地址栏手动输入URL,即使用户没有更改密码,用户仍然可以继续访问该页面.
我用过滤器ForceChangePasswordFilter做了这个.因为如果用户手动键入url,他们可以绕过更改密码表单.使用过滤器,请求始终被截获.
因此,我继续实施自定义过滤器.
我的问题是,当我实现一个自定义过滤器和发送里面重定向,它会通过过滤器再次提到引起无限重定向循环这里.我尝试通过在security-context.xml中声明两个http标签来实现所提到的解决方案,第一个标签具有pattern属性,但它仍然通过我的自定义过滤器:
<http pattern="/resources" security="none"/>
<http use-expressions="true" once-per-request="false"
auto-config="true">
<intercept-url pattern="/soapServices/**" access="permitAll" requires-channel="https"/>
...
<custom-filter position="LAST" ref="passwordChangeFilter" />
</http>
...
<beans:bean id="passwordChangeFilter"
class="my.package.ForcePasswordChangeFilter"/>
<beans:bean id="customAuthenticationSuccessHandler"
class="my.package.CustomAuthenticationSuccessHandler" >
</beans:bean>
<beans:bean id="customAuthenticationFailureHandler"
class="my.package.CustomAuthenticationFailureHandler" >
<beans:property name="defaultFailureUrl" value="/login"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
我当前的实现(有效)是:
isFirstLoginisFirstLogin是否已设置
chain.doFilter()我对此实现的问题是访问我的资源文件夹也会通过此过滤器导致我的页面失真(因为*.js和*.css未成功检索).这就是我<http>在我的安全应用程序context.xml中尝试使用两个标签的原因(这不起作用).
因此,如果servletPath启动或包含"/ resources",我最终必须手动过滤请求.我不希望它像这样 - 必须手动过滤请求路径 - 但现在它就是我拥有的.
这样做的优雅方式是什么?