Tag: servlet filters

如何在Keycloak身份validation之前调用javax.servlet.Filter

我们使用resteasy开发了一个REST API。 (部署在wildfly 10中) 基本上这些REST API是从另一个应用程序内部调用的,端点是用keycloak保护的。 但是一个端点暴露给外部方(该端点也使用keycloak保护)。 但由于外部方无法提供Keycloak Autherization代码,我们已经完成了一个实现,其中客户端使用应用程序生成的auth_key进行注册,客户端将使用该auth_key调用端点。 然后在webfilter(javax.servlet.Filter)中,使用tha auth_key我们获得相关的keycloak authntication Bearer令牌。 如果需要(例如:令牌已过期),我们也会调用Keycloak Server。 收到后,我们将Autherization令牌添加到Webfilter中的httpRequest并继续到终点应用程序。 但问题是, 在Web Filter之前调用KeyCloak身份validation 。 我正在寻找的是“如何在密钥泄露认证之前调用Webfilter?” 编辑: 现在我正试图找到这里提到的方法。 在Keycloak中进行身份validation之前将请求标头设置为请求 。 我可以在身份validation发生之前接听电话。 但是我无法在那里设置请求标头。 web.xml中 Restful Web Application resteasy.scan true keycloak.config.resolver package.to.HeaderBasedKeycloakConfigResolver REST endpoints /ep-name/resource-name resource-name KEYCLOAK realm-name role-name-for-resource-1 role-name-for-resource-2 org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap resteasy-servlet org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher resteasy.servlet.mapping.prefix /ep-name resteasy-servlet /ep-name/* WebFilter package.to.filter.WebFilter WebFilter /desired-ep-name/*

带变量的Servlet映射(Tomcat 7.0)

是否可以将URL映射到servlet(可能是Tomcat特有的),以便后面的两个URL({id}是从代码中检索的变量), /users/{id}/a /users/{id}/b 映射到两个不同的servlet,或者我是否必须为映射到/users/*的servlet实现我自己的某种filter? 更清楚的是,任何带有模式/users/*/a URL都应该映射到同一个servlet。 /users/*/b 。

在filter中访问spring boot数据jpa实体

目前我正在尝试将基于servlet的应用程序转换为带控制器的spring启动应用程序。 设置filter后,我在访问实体函数时遇到“无法初始化代理 – 无会话”-Exception。 (这里:用户方法“isAdmin”)。 我设置了这样的filter: public class AdminFilter implements Filter { @Autowired UserRepository userRepository; @Override @Transactional public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aFilterChain) throws IOException, ServletException { User u = userRepository.getOne(5l).orElse(null); System.out.println(u.isAdmin()); aFilterChain.doFilter(aRequest, aResponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext()); } @Override public void destroy() { } } […]

有什么方法可以预测会话超时?

有没有办法“捕获”会话超时事件,以便在无效之前从HttpSession中检索数据? 我们正在实现filter接口,并且在doFilter方法中,当会话超时时,我们在登录时存储在会话对象中的用户为空。 提前致谢。

Servletfilter – 来自servlet的转发请求是否会转到servletfilter?

如果任何J2EE应用程序直接命中servlet,然后servlet将相同的请求转发到某个.jsp页面。 request.getRequestDispatcher(“Login.jsp”).forward(request, response); 我有一个servletfilter,下面是url-pattern some_filter /* 那么,转发的请求是否也会过滤。 在我的情况下它不会来,这是一个预期的行为。 只想了解这一点。

Spring Web Application已初始化两次

我发现我的spring web项目在tomcat上初始化了两次,这里是打印的消息 第一次: INFO: Initializing Spring root WebApplicationContext INFO 2015-01-08 15:18:04 ContextLoader Root WebApplicationContext: initialization started INFO 2015-01-08 15:18:04 XmlWebApplicationContext Refreshing Root WebApplicationContext: startup date [Thu Jan 08 15:18:04 GMT+08:00 2015]; root of context hierarchy INFO 2015-01-08 15:18:04 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-shiro.xml] INFO 2015-01-08 15:18:04 XmlBeanDefinition Reader Loading XML bean definitions […]

getSession()始终创建一个新会话

我们在应用程序中通过实现Filter来安装SecurityFilter类,我们的doFilter方法如下所示。 public void doFilter(ServletRequest sres, ServletResponse sreq, FilterChain chain) throws IOException, ServletException { LOGGER.debug(Logger.buildLogMessage(“Starting SecurityFilter.doFilter”)); HttpServletRequest request = (HttpServletRequest) sres; HttpServletResponse response = (HttpServletResponse) sreq; HttpSession session = request.getSession(); 我们的web.xml中有以下条目 SecurityFilter com.abcweb.filter.SecurityFilter SecurityFilter /resources/* 我们对应用程序进行了很多REST调用,所有这些调用都通过了这个filter。 Java API文档说,request.getSession()返回会话(如果存在),否则会创建新会话。 但是在我们的应用程序中,request.getSession()总是为每个REST调用创建一个新会话。 这可能会出错?

filter在Struts2中不起作用

由于某种未知的原因,我无法在struts2上使用filter。 我正在使用apache tomcat并使用filter限制用户访问(我喜欢而不是拦截器)。 有没有理由为什么我的/tela-paciente请求没有被过滤? struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* Paciente br.ufg.inf.sas.filtros.FiltroPaciente Paciente /tela-paciente

Java:确保Web应用程序仅在一个浏览器选项卡中打开

确保特定页面(假设单页Web应用程序)仅在一个浏览器选项卡中打开的最佳方法是什么? 假设Java Web应用程序具有身份validation,即用户必须登录(因此我们可以通过Java Session API识别哪个用户正在查看哪个页面)。 目的是如果为同一个URL打开另一个选项卡,则用户将被重定向到静态页面,该页面告诉他应用程序在其他位置打开(另一个选项卡)。 我目前的方法无法在同一浏览器中使用标签,因为JSESSIONID存储在cookie中,可用于所有浏览器的标签。

如何在不向用户显示堆栈跟踪的情况下处理servletfilter中的错误状态?

我正在开发一个Jetty / RESTEasy应用程序。 如果我从我的一个REST端点抛出WebApplicationException(myResponse) ,它会将给定的响应发送到客户端。 当filter检测到错误时,我想要相同的行为: 它应该停止执行,并且 它应该给用户一个明确的,JSON格式的错误,不包括堆栈跟踪。 显然,只需写入响应流并从doFilter方法中return工作。 但这对doFilter调用的其他方法不起作用。 抛出任何exception将满足条件#1,但我还没有想出一个理智的方式来满足条件#2。 (你可以在底部看到我最好的尝试。) 正如Perception在他的回答中解释的那样, WebApplicationException被视为Filter的上下文中的任何其他exception,因此为用户提供了一个很好的丑陋堆栈跟踪。 那么,总结一下我的问题: serveltt容器是否具有throw new WebApplicationException(Response)等价物? 也许更重要的是,其他java项目如何处理这个? 我在一个filter中有这个代码,它可以工作,但我更喜欢一个更优雅的解决方案,它自动应用于所有filter: public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { try { doFilterOrThrow(request, response, chain); } catch (WebApplicationException e) { Response res = e.getResponse(); ((HttpServletResponse) response).sendError(res.getStatus(), (String) res.getEntity()); } }