9

我在 spring boot 1.2.3 web 应用程序中使用 spring security 4.0.1(也使用 spring-session 1.0.1,但这与案例无关)。

我确实有一个私人区域和一个所有用户都可以访问的区域(“/about”、“/”、“/contact”……超过 20 页)。(这就像一个网上商店)

每当登录用户会话过期时,Spring 检测到无效会话并将用户重定向到'.invalidSessionUrl("/session/error/invalid")'

但是,我只想在目标链接在私有区域内时被重定向,而不是在公共区域内。

我怎样才能避免呢?

谢谢。

这是我的(java)配置:(看到帖子后更新)

 http
            .authorizeRequests()
            .anyRequest()
                .permitAll()
            .antMatchers("/privado/**")
                .authenticated()
            .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .successHandler(new SessionSuccessHandler())
            .and()
                .logout()
                .logoutSuccessUrl("/")
                .deleteCookies("JSESSIONID", "SESSION")
            .and()
                .sessionManagement()
                .invalidSessionUrl("/session/error/invalid")
            .sessionFixation()
            .changeSessionId()
            .maximumSessions(1)
            .expiredUrl("/session/error/expired")
            .and()
            .and()
                .csrf()
                .ignoringAntMatchers("/jolokia/**", "/v1.0/**");

我怎样才能做到这一点?

非常感谢。

4

3 回答 3

2

另一个帮助我在与您类似的情况下处理此问题的解决方法是将过期/无效会话策略添加到您的配置中,如下所示:

http
    .expiredSessionStrategy(e -> {
        handleExpiredInvalidSessions(e.getRequest(), e.getResponse());
    })
    .sessionRegistry(sessionRegistry())
    .and()
    .invalidSessionStrategy((request, response) -> {
        handleExpiredInvalidSessions(request, response);
    })

然后您将实现它以匹配公共 URI 并简单地转发请求

private void handleExpiredInvalidSessions(HttpServletRequest request, HttpServletResponse response) {
    String requestUri = request.getRequestURI();
    if (isPublicURI(requestUri)) {
        // This will remove the invalid/expired session from the request
        // and prevent the request from failing again
        request.getSession(true).invalidate();
        RequestDispatcher dispatcher = request.getRequestDispatcher(requestUri);
        // Retry the request
        dispatcher.forward(request, response);
    } else {
        // might redirect if you wish
        response.setStatus(440);
    }
}

你仍然需要isPublicURI()根据你想要的公共路径来实现,在我的例子中它只有一个路径,所以它很容易。

于 2019-11-29T14:20:26.000 回答
2

@RobWinch - 这似乎是一个非常常见的用例,您提出的解决方案似乎不适用于我运行的测试和评论。提出了类似的问题,我相信http://forum.spring.io/forum/spring-projects/security/94772-redirect-to-invalid-session-url-only-when-user-accesses-secured-resource和它似乎它从未得到解决。我的想法是有多个http设置(使用xml配置)

<http pattern="/aboutUs**" security="none" />
<http pattern="/contact**" security="none" />
etc

当有相当多的不安全页面并且添加新的不安全页面需要配置更新时,这似乎并不理想。如果我们能为这个用例提供一个“理想”的解决方案,那就太好了。在 Spring security 4.1 版本中,似乎仍然没有明确的方法来做到这一点。

于 2016-08-18T01:27:45.573 回答
0

您可以提供自定义 SessionAuthenticationStrategy 来执行此操作。例如:

public class MatcherSessionAuthenticationStrategy implements SessionAuthenticationStrategy {

    private final SessionAuthenticationStrategy delegate;

    private final RequestMatcher matcher;

    public MatcherSessionAuthenticationStrategy(
            SessionAuthenticationStrategy delegate, RequestMatcher matcher) {
        super();
        this.delegate = delegate;
        this.matcher = matcher;
    }

    public void onAuthentication(Authentication authentication,
            HttpServletRequest request, HttpServletResponse response)
            throws SessionAuthenticationException {
        if(matcher.matches(request)) {
            delegate.onAuthentication(authentication, request, response);
        }
    }
}

然后你可以将 RequestMatcher 和 ConcurrentSessionControlAuthenticationStrategy 注入到类中。配置它的最简单方法是创建一个 BeanPostProcessor:

public class ConcurrentSessionControlAuthenticationStrategyBeanPostProcessor
        implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        if(!(bean instanceof CompositeSessionAuthenticationStrategy)) {
            return bean;
        }

        RequestMatcher matcher = antMatchers("/about", "/","/contact");
        SessionAuthenticationStrategy original = (SessionAuthenticationStrategy) bean;
        return new MatcherSessionAuthenticationStrategy(original, matcher);
    }

    /**
     * Create a {@link List} of {@link AntPathRequestMatcher} instances.
     *
     * @param httpMethod the {@link HttpMethod} to use or {@code null} for any
     * {@link HttpMethod}.
     * @param antPatterns the ant patterns to create {@link AntPathRequestMatcher}
     * from
     *
     * @return an OrRequestMatcher with a {@link List} of {@link AntPathRequestMatcher} instances
     */
    public static RequestMatcher antMatchers(
            String... antPatterns) {
        List<RequestMatcher> matchers = new ArrayList<RequestMatcher>();
        for (String pattern : antPatterns) {
            matchers.add(new AntPathRequestMatcher(pattern));
        }
        return new OrRequestMatcher(matchers);
    }
}

然后,您可以将以下内容添加到您的配置中:

@Bean
public static BeanPostProcessor sessionBeanPostProcessor() {
    return new ConcurrentSessionControlAuthenticationStrategyBeanPostProcessor();
}

使用静态方法很重要,因为这是一个需要尽早初始化的 BeanPostProcessor。

PS 我会考虑按照本博客中所述格式化您的配置

于 2015-06-17T21:24:19.933 回答