春季安全loadUserByUsername用户名用户名在几天未使用后为空

时间:2019-03-20 12:14:14

标签: spring-security spring-session

d %>% 
  mutate(figures = replace(figures, region == 'all', 10)) %>%
  mutate(figures2 = replace(figures2, region == 'all', 10))

# A tibble: 4 x 3
  region figures figures2
  <chr>    <dbl>    <dbl>
1 all         10       10
2 nj           7        5
3 rkl          4        6
4 all         10       10

} 我将这个配置用于我的春季安全性,并首先获得了成功的登录信息。 但是随后我们发现,如果几天内没有人访问服务器(甚至登录URL),然后再次登录,则loadUserByUsername用户名将为空,并且登录失败

仅供参考,我通过添加依赖项将redis用于会话管理

@Autowired
private BCryptPasswordEncoder passwordEncoder;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .userDetailsService(cUserDetailService)
            .passwordEncoder(passwordEncoder);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/user/**").authenticated()
            .and()
            .csrf().disable()
            .formLogin()
            .usernameParameter("username")
            .passwordParameter("password")
            .successHandler(successHandler)
            .failureHandler(failureHandler)
            .and()
            .logout()
            .logoutSuccessHandler(logoutSuccessHandler)
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(authEntryPoint)
            .accessDeniedHandler(accessDeniedHandler)
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
            .maximumSessions(1)
            .maxSessionsPreventsLogin(false);
}


public class CUserDetailService implements UserDetailsService {

@Autowired
private UserDetailDataEntityRepository userDetailDataEntityRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    log.info("username: " + username + ", " );
    log.info(username == null);

    Optional<UserDetailDataEntity> optional = userDetailDataEntityRepository.findByUsername(username);
    if (optional.isPresent()) {
        return optional.get();
    } else {
        throw new UsernameNotFoundException(username);
    }
}

并且登录请求是表单请求

0 个答案:

没有答案