Spring MVC/安全 - "Store is closed"安全过滤器

标签 spring spring-security

我有一个使用 Spring Security 的 Spring MVC Web 应用程序。我想添加“商店已关闭”功能 - 即,如果我将商店设置为关闭,则无论用户尝试在我的网站上的哪个位置导航,他们最终都会在页面上显示“商店已关闭”。

我已经实现了一个安全过滤器,如下所示(并且可以很好地连接它):

public class ClosedFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest req,
        HttpServletResponse response,
        FilterChain chain) throws ServletException, IOException {

    if(storeIsClosed()) {
        //do something useful here
    }else {
        chain.doFilter(req, response);
            }

}

}

但我不确定在“//在这里做一些有用的事情”位中应该放什么。我尝试过:

throw new StoreIsClosedException();  //extends RuntimeException

但我无法将异常映射到我的 View 。我也尝试过

response.redirect("myClosedView");

没有运气。我想要的概念是这样的:

   return new ModelAndView("closed");

谢谢。

最佳答案

我最终找到了一个解决方案,我将过滤器更改为:

public class ClosedFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest req,
        HttpServletResponse response,
        FilterChain chain) throws ServletException, IOException {

    if (isClosed()) {
        final String path = req.getContextPath()+"/closed.do";
        if(!path.equals(req.getRequestURI())) {
            response.sendRedirect(path);
            return;
        }
    }
    chain.doFilter(req, response);
}


}

然后将其添加到我的 security.xml

<intercept-url pattern="/closed.do*" access="permitAll"/>

关于Spring MVC/安全 - "Store is closed"安全过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12828451/

相关文章:

java - Spring配置问题

java - 如何在 vaadin 中使用复习插件?

spring - org.springframework.beans.NotReadablePropertyException : Invalid property of bean class Bean property is not readable or has an invalid getter method

java - 401 而不是 403 与 Spring Boot 2

java - 我的 Spring-Boot 自定义登录表单不起作用 [更新]

java - 在每个 http 请求上向客户端发送 stomp 消息

java - 使用 Spnego/Kerberos 的 Spring Boot - 配置问题 - 需要 ServletContext 来配置默认的 servlet 处理

java - 通过 Java 配置禁用 Spring Security?

java - 尝试构建示例 Red5 Secure 项目,但由于某些依赖项而失败

java - 将 Spring Security 2.5 升级到 3.1 - java.lang.NoClassDefFoundError : org/springframework/security/Authentication