会话在春季不会破坏浏览器关闭事件

时间:2018-02-16 10:56:59

标签: spring spring-mvc spring-boot spring-security spring-session

我使用spring boot开发一个小应用程序。我的问题是,当用户正确验证时,我需要显示用户的名字。每次身份验证(每次登录 - 输入用户名和密码)都会正确显示名字。但是,如果我们关闭浏览器并在会话超时之前重新打开它而不输入用户名和密码 ,则不会显示名字。

认证时我的混淆

@Component
public class SecurityHandler implements AuthenticationSuccessHandler{

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
        HttpSession session = request.getSession();

        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((UserDetails) principal).getUsername();
        } else {
            userName = principal.toString();
        }
        User user = userService.findBySSO(userName);        

        session.setAttribute("userName", user.getFirstName()); 
        response.sendRedirect(request.getContextPath()+"/dashboard/index");

    }

}

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    //Autowired

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers() // antmachers 
        .and().formLogin().loginPage("/login").successHandler(securityHandler).loginProcessingUrl("/login").usernameParameter("ssoId").passwordParameter("password")
        .and().rememberMe().rememberMeParameter("remember-me").tokenRepository(tokenRepository)
        .tokenValiditySeconds(86400).and().csrf().and().exceptionHandling().accessDeniedPage("/Access_Denied")
        .and()
        .sessionManagement().invalidSessionUrl("/login").maximumSessions(1).expiredUrl("/login").and().sessionAuthenticationErrorUrl("/login").sessionFixation().migrateSession()
        .sessionCreationPolicy(SessionCreationPolicy.ALWAYS); 

        http.logout()
        .logoutUrl("/logout")
        .logoutSuccessUrl("/login")
        .invalidateHttpSession(true)
        .permitAll();
    }
}

会话列表器

public class SessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent event) {            
        event.getSession().setMaxInactiveInterval(-1);          
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {          
    }

}

我提到Java:Why http session is not destroyed when tab or browser is closed?,因为他们说我尝试使用javascritp通过 onunload事件来调用注销,但它不起作用。很少有教程说它的工作取决于浏览器的设置。

最后无论如何,如果用户使用任何方法进入网站,我都需要显示用户的名字。任何人,如果你知道,请帮助我,这让我很头疼。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您的会话监听器应该是我认为的bean

你可以试试这个:

@WebListener
public class SessionListener implements HttpSessionListener{

@Override
public void sessionCreated(HttpSessionEvent se) {
    System.out.println("Session created");
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    System.out.println("session Destroyed");
}

}

答案 1 :(得分:0)

您应该访问包含用户名字的网页,该网页仅限于授权用户,您的用户在关闭浏览器后退出,因为您没有看到他们的名字。
你错误地理解了这个问题,从而没有得到正确的研究方法。希望这有帮助

相关问题