2016-04-19 8 views
0

jhipsterを使用してWebアプリケーションを書き込んでいます。そしてそれは春を使用しています。私は、同じユーザーが自分のアプリケーションにログインできる回数を制限しようとしていた、それはこれでServerConfiguration.javaという名前のファイルに動作するようになった:アプリケーションでの最大セッション数ではなく、アプリケーションでの最大セッション数ではありません。

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
    .and() 
     .formLogin() 
     .loginProcessingUrl("/api/authentication") 
     .successHandler(ajaxAuthenticationSuccessHandler) 
     .failureHandler(ajaxAuthenticationFailureHandler) 
     .usernameParameter("j_username") 
     .passwordParameter("j_password") 
     .permitAll() 
    . 
    . 
    . 
    . 
    .and()    
     .sessionManagement() 
     .maximumSessions(Integer.parseInt(env.getProperty("spring.maxuser.sessions"))) 
       .maxSessionsPreventsLogin(true); 
    } 


@Bean 
public HttpSessionEventPublisher httpSessionEventPublisher() { 
    return new HttpSessionEventPublisher(); 
} 

これは、特定のユーザーだけに何度もログインします私のアプリケーション。

今、私の質問は、x番のdifferentユーザーのためだけに自分のアプリケーションを開いてアクセスできるようにする方法です。たとえば、私のアプリケーションに200人のユーザーしかアクセスしないようにしたいとします。ユーザ201が来てログインしようとすると、それはできません。

私はこの他の投稿spring limit max sessions ; limit max usersの投稿を見ましたが、私はこのコードを正確にどこに置くべきかわかりません。

public class MySessionAuthenticationStrategy extends ConcurrentSessionControlStrategy { 
int MAX_USERS = 1000; // Whatever 
SessionRegistry sr; 

public MySessionAuthenticationStrategy(SessionRegistry sr) { 
    super(sr); 
    this.sr = sr; 
} 

@Override 
public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) { 
    if (sr.getAllPrincipals().size() > MAX_USERS) { 
     throw new SessionAuthenticationException("Maximum number of users exceeded"); 
    } 
    super.onAuthentication(authentication, request, response); 
} 

}

私はこの新しいクラスMySessionAuthenticationStrategy作成する必要がありますし、どのように私は、この新しいクラスに私のhttpConfigureクラスから行くのですかMySessionAuthenticationStrategy

はどうもありがとうございました。

答えて

0

これを試してください。 デフォルトのセッションレジストリを拡張するクラスを作成します。

@Component 
public class MySessionRegistry extends org.springframework.security.core.session.SessionRegistryImpl {  
} 

更新しますconfigureメソッドは次のようになります。私はこれが役に立てば幸い

@Autowired 
    MySessionRegistry sessionRegistry; 

    public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) { 
     if (calculateMaxSessions(sessionRegistry) > MAX_USERS) { 
      throw new SessionAuthenticationException("Maximum number of users exceeded"); 
     } else { 
      //Authenticate 
     } 
    } 

    public int calculateMaxSessions(SessionRegistry sessionRegistry){ 
     final List<Object> principals = sessionRegistry.getAllPrincipals(); 
     if (principals != null) { 
      List<SessionInformation> sessions = new ArrayList<>(); 
      for (Object principal : principals) { 
       sessions.addAll(sessionRegistry.getAllSessions(principal, false)); 
      } 
      return sessions.size(); 
     } 
     return 0; 
    } 

ログイン/認証時に続いて
@Autowired 
    MySessionRegistry sessionRegistry; 
    void configure(HttpSecurity http) throws Exception { 
     http.formLogin() 
       .loginProcessingUrl("/api/authentication") 
       .successHandler(ajaxAuthenticationSuccessHandler) 
       .failureHandler(ajaxAuthenticationFailureHandler) 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .permitAll().and() 
       .sessionManagement() 
       .maximumSessions(Integer.parseInt(env.getProperty("spring.maxuser.sessions"))) 
       .sessionRegistry(sessionRegistry) 
       .maxSessionsPreventsLogin(true); 
    } 

、これを試してみてください。乾杯!

+0

ご回答いただきありがとうございます。 1-この空のクラスを作成するだけです 2次に、私の新しく作成したクラスの新しいインスタンスを 'SecurityConfiguration.java'カルルスに追加します。私はAutowiredがどのように正確に動作するのか分かりません。この新しい属性を追加してください: 'sessionsRegistry(myNewlyCreatedClassInstance)' 3ログイン/認証の正確な場所はわかりません.... :(これは私がこの春のものを見るのは初めてです。 ? もう一度ありがとうございます – Torre

関連する問題