Spring WebMVC - 成功登录后如何连接拦截器进行EULA检查?

时间:2015-04-11 23:26:18

标签: java spring spring-mvc spring-security

我使用Spring MVC和Security来验证我的网络服务。

我希望系统检查任何新认证的用户是否已接受EULA。如果EULA检查是好的,那就让他们继续他们的快乐方式。

如果EULA检查失败,那么让他们停在" / eula"页面,直到他们接受。

我有以下课程:

@Component
public class AssertEulaInterceptor extends HandlerInterceptorAdapter {

    @Value("eulaCookieName")
    private String cookieName;

    @javax.annotation.Resource(name = "EULAResource")
    private Resource eulaResource;

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("EULA CHECK");
        Cookie cookie = extractCookie(cookieName, request.getCookies());

        if (assertEulaAccepted(request, response, handler, cookie)) {
            response.sendRedirect("/services/eula");
        }
    }
}

并在我的servlet-context.xml文件中:

...
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/static/j_spring_security_check*" />
        <bean class="com.example.AssertEulaInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>
...

我也尝试过:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/static/j_spring_security_check*" />
        <mvc:exclude-mapping path="/login*"/>
        <mvc:exclude-mapping path="/eula*"/>
        <bean class="com.example.AssertEulaInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

但这并没有做检查。 (&#34; EULA CHECK&#34;永远不会出现在控制台输出中。)

感谢。

1 个答案:

答案 0 :(得分:0)

使用排除映射

    <mvc:interceptor>
        <mvc:mapping path="/check-me-pattern"/>
        <mvc:exclude-mapping path="/exclude-me-pattern"/>
        <bean class="com.example.MyInterceptor"/>
    </mvc:interceptor>