Не удается автоматически извлечь securityContext из сеанса redis - PullRequest
0 голосов
/ 29 мая 2019

Попытка собрать базу приложения отдыха при загрузке Spring. И я настраиваю свой процесс входа в систему, я следую руководству, чтобы вручную установить контекст безопасности, и он отлично работает:

UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(email, password);
Authentication authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
UserDetails userDetails = customUserDetailsService.loadUserByUsername(email);
List<String> roles = new ArrayList<String>();
for (GrantedAuthority authority : userDetails.getAuthorities()) {
                roles.add(authority.toString());
            }
// Set security context to session
session.setAttribute("SPRING_SECURITY_CONTEXT",SecurityContextHolder.getContext());

Но когда я пытаюсь вернуть свой аутентификатор, он всегда получает anonymousUser из этого кода

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();

Если я вручную установлю сессию, все будет хорошо, но я не думаю, что это правильное решение:

SecurityContext securityContext = (SecurityContext)servletRequest.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
SecurityContextHolder.setContext(securityContext);

Моя конфигурация идет сюда:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig<S extends Session> extends WebSecurityConfigurerAdapter {
    // The max length is 31. The bigger the integer is the longer the time will
    // take.
    // The default value is 10.
    final int ENCODER_STRENTH = 11;

    @Autowired
    @Qualifier("authTokenConfig")
    private AuthTokenConfig authTokenConfig;

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // This application is based on RESTful structure, don't need additional CSRF
        // protect.
        http.csrf().disable();
        // The pages does not require login
        http.authorizeRequests().antMatchers("/", "/user/registration", "/login/authenticate", "/login/").permitAll();
        http.apply(authTokenConfig);
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.sessionManagement().sessionFixation().migrateSession();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder.userDetailsService(customUserDetailsService).passwordEncoder(getEncoder());
    }

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

    @Bean
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Autowired
    private FindByIndexNameSessionRepository<S> sessionRepository;

    @Bean
    public SpringSessionBackedSessionRegistry<S> sessionRegistry() {
        return new SpringSessionBackedSessionRegistry<>(sessionRepository);
    }

Конфигурация redis:

@Configuration
//set the Redis session expiration time
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 302400)
public class Config {

    @Value("${spring.redis.host}")
    private String redisServer;
    @Value("${spring.redis.port}")
    private int redisPort;
    @Value("${spring.redis.password}")
    private String redisPassword;

    // Create a RedisConnectionFactory that connects Spring Session to the Redis
    // Server.
    @Bean
    @Primary
    public LettuceConnectionFactory redisConnectionFactory() {
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().build();
        RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(redisServer, redisPort);
        serverConfig.setPassword(redisPassword);
        return new LettuceConnectionFactory(serverConfig, clientConfig);
    }
     @Bean
     public HttpSessionEventPublisher httpSessionEventPublisher() {
             return new HttpSessionEventPublisher();
     }

    // customize Spring Session’s HttpSession integration to use HTTP headers to
    // convey the current session information instead of cookies.
    @Bean
    public HttpSessionIdResolver httpSessionIdResolver() {
        return HeaderHttpSessionIdResolver.xAuthToken(); 
    }

}

инициализатор

public class Initializer extends AbstractHttpSessionApplicationInitializer {

}

Я следую руководству по загрузке пружин и проверял много раз, не имею понятия, пожалуйста, помогите мне выяснить, где это может пойти не так. Спасибо.

...