Spring Security无效会话重定向

jos*_*ete 5 spring-mvc spring-security expired-sessions spring-boot

我在Spring Boot 1.2.3 Web应用程序(以及spring-session 1.0.1,中使用spring security 4.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/**");
Run Code Online (Sandbox Code Playgroud)

我该如何实现?

非常感谢。

Rob*_*nch 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以将 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

@Bean
public static BeanPostProcessor sessionBeanPostProcessor() {
    return new ConcurrentSessionControlAuthenticationStrategyBeanPostProcessor();
}
Run Code Online (Sandbox Code Playgroud)

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

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


归档时间:

查看次数:

10507 次

最近记录:

6 年,3 月 前