期限切れのセッションでNullPointerExceptionが発生

デビッドAntelo:

私は春を使用している、と私の更新は、ユーザーが言ったとき、私は、ユーザーのセッションを期限切れにする必要があります。私は、以下の構成を使用しています:

@Bean
@Override
public AuthenticationManager authenticationManagerBean () throws Exception {

    return super.authenticationManagerBean();

}

@Bean
public SessionRegistry sessionRegistry () {

    return new SessionRegistryImpl();

}
@Bean
public ServletListenerRegistrationBean httpSessionEventPublisher() {    //(5)
    return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
}

@Override
public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {

    authenticationMgr.userDetailsService(inMemoryUserDetailsManager());

}

@Override
protected void configure (HttpSecurity http) throws Exception {

    http.authorizeRequests()
        .antMatchers("*.jsp").authenticated()
        .and()
            .formLogin().loginPage("/login.html")
            .defaultSuccessUrl("/")
            .failureUrl("/login.html?failed=1")
            .usernameParameter("email").passwordParameter("password")               
        .and()
            .logout().logoutUrl("/logout.html")
        .and()
            .logout().logoutSuccessUrl("/")
        .and()
            .sessionManagement()
            .maximumSessions(100)
            .maxSessionsPreventsLogin(true)
            .expiredUrl("/ejercicios-programacion/")
            .sessionRegistry(sessionRegistry());

}

そして、これは私がセッションを期限切れにする方法です。

public void expireUserSessions(String username) {
    for (Object principal : sessionRegistry.getAllPrincipals()) {
        if (principal instanceof User) {
            UserDetails userDetails = (UserDetails) principal;
            if (userDetails.getUsername().equals(username)) {
                for (SessionInformation information : sessionRegistry.getAllSessions(userDetails, false)) {
                    information.expireNow();
                }
            }
        }
    }
}

これが行われ、私は私が私の更新されたユーザを持っていたブラウザ上でページをリロードした場合、それは例外が表示されます:

java.lang.NullPointerException
org.springframework.security.web.session.ConcurrentSessionFilter$1.onExpiredSessionDetected(ConcurrentSessionFilter.java:107)

これにリダイレクトします。

@Override
public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
    HttpServletRequest request = event.getRequest();
    HttpServletResponse response = event.getResponse();
    SessionInformation info = event.getSessionInformation();

    redirectStrategy.sendRedirect(request, response, determineExpiredUrl(request, info));
}

特に、それは、コードの最後の行は、例外をスロー一つです。私は再びページをリロードした場合、例外を取得した後、その後、すべてが正常です。私は例外を取得しないと私はログアウトしています。私はなぜこれが起こっている見当がつかない。誰もが知っていますか?

デビッドAntelo:

[OK]を、私は最終的にこの問題を解決するために管理します。答えは1が廃止され、デフォルトで使用されている方法の多くは、独自のConcurrentSessionFilterを使用することです。このBeanを追加します。

 @Bean public ConcurrentSessionFilter concurrentSessionFilter() {

    ConcurrentSessionFilter c = new ConcurrentSessionFilter(sessionRegistry(), new SessionInformationExpiredStrategy() {

        @Override
        public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {

            HttpServletRequest request = event.getRequest();
            HttpServletResponse response = event.getResponse();
            SessionInformation info = event.getSessionInformation();

            redirectStrategy().sendRedirect(request, response, "/ejercicios-programacion/");

        }
    });

    return c;

}

そして、あなたは、私はインデックスページにユーザーを移動するために私の新しいRedirectStrategyを使用し、私の場合は、上書きされた方法でやりたいです。

そして、あなたの設定方法にこれを追加します。

protected void configure (HttpSecurity http) throws Exception {

    ...
    // Whatever you want to configure

    http.addFilterBefore(concurrentSessionFilter(), ConcurrentSessionFilter.class);

}

私はこれがいかに直感を信じることができない、私は期限切れのセッションのような簡単なものは春に一生懸命と鈍角ことができる方法がわかりません

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=279462&siteId=1