如何在Spring Session中使用jcaptcha?

3
我们实现了由Redis支持的Spring Session,并拥有一组Tomcat服务器。当我们通过不设置jvmRoute来关闭粘性会话时,我们在jcaptcha服务中不断收到“文本验证失败”的错误信息。我认为这是因为jcaptcha servlet并不知道Spring Dispatcher servlet的存在,后者具有所有Spring Session过滤器,因此无法读取会话变量。我们该如何让jcaptcha与Spring Session配合工作呢?
以下是我们的设置:
Web.xml
<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>jcaptcha</servlet-name>
    <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>jcaptcha</servlet-name>
    <url-pattern>/jcaptcha/jcaptcha.jpg</url-pattern>
</servlet-mapping>

CustomHttpSessionAppInitializer.java

public class CustomHttpSessionAppInitializer extends AbstractHttpSessionApplicationInitializer {}

RedisSessionConfig.java

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {

    @Value("${spring.redis.host}")
    private String redisServerName;

    @Value("${spring.redis.port}")
    private Integer redisServerPort;

    @Value("${spring.redis.database}")
    private Integer redisServerDatabase;

    @Value("${spring.redis.password}")
    private String redisServerPassword;

    @Value("${spring.server.affinity}")
    private Boolean isServerAffinity = Boolean.FALSE;

    @Autowired
    private SessionIdentifierService sessionIdentifierService;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisServerName, redisServerPort);
        config.setDatabase(redisServerDatabase);
        config.setPassword(RedisPassword.of(redisServerPassword));
        return new JedisConnectionFactory(config);
    }

    /*
     * We need to register every HttpSessionListener as a bean to translate SessionDestroyedEvent and SessionCreatedEvent into
     * HttpSessionEvent. Otherwise we will got a lot of warning messages about being Unable to publish Events for the session.
     * See Spring Session Docs at:
     * {@link} https://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession-httpsessionlistener
     */
    @Bean
    public HttpSessionEventPublisher httpSessionEventPublisher() {
        return new HttpSessionEventPublisher();
    }

    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setUseBase64Encoding(false);
        if (isServerAffinity) {
            serializer.setJvmRoute(sessionIdentifierService.getJvmRoute());
        }
        return serializer;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}
1个回答

0

将jcaptcha与Spring Session集成应该没有问题。只要有一种从Redis加载会话的方法(在这种情况下通过SESSION cookie),并且会话存在,调用request.getSession()request.getSession(false)将返回基于Redis的会话。

这适用于任何在springSessionRepositoryFilter之后调用的过滤器和servlet。如果查看SessionRepositoryFilter的源代码,您将看到HttpServletRequest被替换为SessionRepositoryRequestWrapper

因此,您的SimpleImageCaptchaServlet和用于验证用户响应的任何servlet都将获得一个SessionRepositoryRequestWrapper,它将无缝地为您提供对基于Redis的会话的访问。

问题可能出在您的配置上,特别是因为您同时使用了web.xml和Servlet 3.0+ WebApplicationInitializer, 导致springSessionRepositoryFilter没有向容器注册。如果您的应用程序正常工作,则您的web.xml很可能正常工作。您是否使用WebApplicationInitializer来加载您的web.xml?如果没有,那么可能是您的Java配置文件没有加载。请确保您的web.xml以某种方式加载您的配置,例如通过在contextLoaderListener xml配置文件中启用组件扫描(<context:component-scan/>),以及加载您的Java配置文件:
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

加载配置以创建过滤器,然后您必须将其添加到您的web.xml中:

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

请查看Spring Session XML配置参考文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接