【问题标题】:Session doesn't exist in spring春季不存在会话
【发布时间】:2018-01-29 06:20:33
【问题描述】:

我正在使用spring boot开发一个应用程序,这里我想显示登录用户的“名称和图像”,所以我使用会话来传递名称和图像认证后。它的工作如果任何用户输入用户凭据(在登录页面)或任何登录用户直接输入 URL 几分钟 (www.abc.com/this/url)时间>。但几分钟后,会话名称和图像 不可见(会话已过期)但其他功能正在使用该会话。我的代码是

@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());  
        session.setAttribute("imgPathh", user.getImagePath()); 

        response.sendRedirect(request.getContextPath()+"/dashboard/index");

    }

}

通用jsp页面

<h2><c:out value="${userName }"></c:out></h2>

我想知道为什么这个会话变量在认证几分钟后仍然不起作用(无论如何,如果我们直接输入 URL,它应该通过这个认证,对吗?)

更新 1。 安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("customUserDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    PersistentTokenRepository tokenRepository;

    @Autowired
    SecurityHandler securityHandler;

    @Autowired
    HttpSession session;

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
        auth.authenticationProvider(authenticationProvider());


    }

    @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().sessionFixation().migrateSession()
        .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED); //always, IF_REQUIRED,never ,stateless 

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

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        authenticationProvider.setHideUserNotFoundExceptions(false);
        System.out.println("Error in DaoAuthenticationProvider");
        return authenticationProvider;
    }

    @Bean
    public PersistentTokenBasedRememberMeServices getPersistentTokenBasedRememberMeServices() {
        PersistentTokenBasedRememberMeServices tokenBasedservice = new PersistentTokenBasedRememberMeServices(
                "remember-me", userDetailsService, tokenRepository);
        System.out.println("Error in PersistentTokenBasedRememberMeServices");
        return tokenBasedservice;
    }

    @Bean
    public AuthenticationTrustResolver getAuthenticationTrustResolver() {
        System.out.println("Error in AuthenticationTrustResolver");
        return new AuthenticationTrustResolverImpl();
    }

}

【问题讨论】:

  • 你在使用spring-security吗?您的安全配置是什么?
  • 尝试在web.xml中设置会话超时
  • @VPK 是的,我在这里更新了我的代码,请检查
  • but other function are working with that session 是什么意思?会话中是否只有userNameimgPathh 不可用?

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


【解决方案1】:

这称为会话超时。

一旦会话超时或过期,就是这样。

用户在服务器上没有任何会话了。

用户必须重新登录。

如果您希望保留更长时间,请尝试更改会话超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多