【发布时间】:2021-10-29 07:26:36
【问题描述】:
我正在使用 spring-boot-starter-web 以及 embedded-jetty 和 starter-jersey 启动器。 Jersey servlet 上下文路径在 application.properties 中配置为从/api 提供服务。所以所有的/api/.* 电话都被交给了泽西岛。
由于我使用的是starter-web,因此静态内容是从static/ 目录提供的,如下所示:
static/public/ 下列出的所有资源都可以不受任何限制地访问。但是static/private下的资源应该是受限的,只有登录才会显示。
为此,我编写了一个过滤器:
@Component
@Order(1)
public static class PrivateContentFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
if (request.getRequestURI().matches(".*/static/private/.*")) {
// Check for authentication in the cookie and procceed
// The cookie is handed to an auth mirco-service, that does the actual validation.
}
}
}
但只有当路径为api/.* 而不是静态内容:/public/.* 或/private.* 时,才能到达此过滤器。我在这里错过了什么?
【问题讨论】:
-
为什么不使用 Spring Security?
-
仅仅做一个简单的过滤器可以做的事情似乎有点矫枉过正,不是吗?我在项目中没有使用spring security,所以我正在寻找解决方案而无需安装额外的库。
-
你最终会开发出已经用 Spring Security 解决的东西。 Cookie 处理、身份验证等
-
但是您假设我的 spring 应用程序将负责 cookie 管理和身份验证。事实并非如此。就像我说的那样,我正在寻找一种解决方案来避免维护额外的库。
-
在你的问题中我看到://检查 cookie 中的身份验证并继续
标签: java spring spring-boot spring-mvc servlets