2017-06-08 36 views
0

新しい要件に従って、カスタムUsernamePasswordAuthenticationFilterを作成して、ログインページから追加のパラメータを取得しました。予想通り、私の設定はうまくいきました。フィルタで追加のパラメータを取得してセッションに保存できるようになりました。しかし、私のカスタムフィルターをconfigに追加した後、セッション管理は機能しません。以前、私はmax sessionsの値を1に設定することで、ユーザーごとに1セッションしか許可しませんでした。今は動作していません。アプリケーションは同じユーザーに複数回のログインを許可しています。私はそれが私の設定にカスタムUsernamePasswordAuthenticationFilterを統合した後に起こっていると確信しています。以下は私の春のセキュリティ設定です。カスタムUsernamePasswordAuthenticationFilterを使用したSpringセキュリティの並行性制御

http.formLogin() 
      .loginPage("/login.html") 
      .loginProcessingUrl("/login.html") 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .and() 
     .logout() 
      .logoutSuccessUrl("/login.html") 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout.html")) 
      .invalidateHttpSession(true) 
      .deleteCookies("JSESSIONID") 
      .and() 
     .sessionManagement() 
      .maximumSessions(1) 
      .expiredUrl("/multiplesessions.html") 
      .sessionRegistry(getSessionRegistry()); 
     http.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); 


@Bean 
public SessionRegistry getSessionRegistry() { 
    return new SessionRegistryImpl(); 
} 
@Autowired 
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 

    DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); 
    provider.setUserDetailsService(dsnyUserDetailsService); 
    provider.setPasswordEncoder(passwordEncoder()); 
    auth.authenticationProvider(provider); 
} 

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

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

@Bean 
DsnyUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception { 
    DsnyUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new DsnyUsernamePasswordAuthenticationFilter(); 
    customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean()); 
    customUsernamePasswordAuthenticationFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login.html", "POST")); 

    return customUsernamePasswordAuthenticationFilter; 
} 

ここには何か不足していますか?

答えて

0

カスタムConcurrentSessionFilterを追加してこの問題を解決しました。もし誰かが望むなら、ここにコードがあります。

http.sessionManagement().sessionAuthenticationStrategy(concurrentSession()); 
    http.addFilterBefore(concurrentSessionFilter(), ConcurrentSessionFilter.class); 

    @Bean 
    public CompositeSessionAuthenticationStrategy concurrentSession() { 

      ConcurrentSessionControlAuthenticationStrategy concurrentAuthenticationStrategy = new ConcurrentSessionControlAuthenticationStrategy(getSessionRegistry()); 
      concurrentAuthenticationStrategy.setMaximumSessions(1); 
      //concurrentAuthenticationStrategy.setExceptionIfMaximumExceeded(true); 
      List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>(); 
      delegateStrategies.add(concurrentAuthenticationStrategy); 
      delegateStrategies.add(new SessionFixationProtectionStrategy()); 
      delegateStrategies.add(new RegisterSessionAuthenticationStrategy(getSessionRegistry())); 

      CompositeSessionAuthenticationStrategy authenticationStrategy = new CompositeSessionAuthenticationStrategy(delegateStrategies); 
      return authenticationStrategy; 
    } 

    @Bean 
    ConcurrentSessionFilter concurrentSessionFilter() { 
      CustomSessionInformationExpiredStrategy redirectStrategy = new CustomSessionInformationExpiredStrategy("/pub/multiplesessions.html"); 
      CustomConcurrentSessionFilter concurrentSessionFilter = new CustomConcurrentSessionFilter(getSessionRegistry(), redirectStrategy); 
      return concurrentSessionFilter; 
    } 
関連する問題