2016-11-18 5 views
0

私は春、春のセキュリティと休止状態で基本的なティックタックトゥのプロジェクトを作っています。アプリケーションは、ログに記録された各ユーザーのゲームをデータベースに保存し、いつでもロードすることができます。これは問題ではありませんが、マルチスレッドの場合には現れます。単一のアプリケーションを単一のブラウザウィンドウで実行すると、すべてがうまくいきます。しかし、別のウィンドウを開くと、2人のプレイヤーが同じゲームをしています。春のセキュリティ複数のログに記録されたユーザーが失敗する

私は、Springによって作成されたBeanシングルトンが原因である可能性がありますが、それはないと確信しています。現在ログインしているユーザーを確認するには、私はSecurityContextHolder複数のユーザーがログインしている

private User getUserFromSpringContext() { 
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
    String name = authentication.getName(); 
    System.out.println("Currently logged users = " + name); 
    return userService.findUserByUsername(name); 
} 

、それらの一つだけのmetodプリント名から彼を取得するための方法を作りました。なぜ私は考えていない。ここではいくつかの重要なコード行私のセキュリティの構成とuserDetailsクラスは、次のとおりです。

セキュリティ設定:

@Autowired 
UserDetailsService userDetailsService; 

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

@Bean 
public DaoAuthenticationProvider authenticationProvider() { 
    DaoAuthenticationProvider auth = new DaoAuthenticationProvider(); 
    auth.setUserDetailsService(userDetailsService); 
    return auth; 
} 

カスタムユーザーの詳細サービス

@Autowired 
private UserService userService; 

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
    User user = userService.findUserByUsername(username); 
    if (user == null) { 
     throw new UsernameNotFoundException("Username not found"); 
    } 
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), true, true, true, true, getAuthoriries(user)); 
} 

public void setUserService(UserService userService) { 
    this.userService = userService; 
} 

private List<GrantedAuthority> getAuthoriries(User user) { 
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 
    authorities.add(new SimpleGrantedAuthority(user.getRole().getRole())); 
    return authorities; 
} 

誰もがこの問題の原因を知っていますか?

テスト中に別の問題が発生しました。ログアウトをクリックすると、すべてのユーザーがログアウトします。私はSpring Security Configurationの残りの部分をここに掲載しています。

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
      .antMatchers("/start", "/", "/login", "/registry","/success","/new").permitAll() 
      .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") 
      .antMatchers("/**").access("hasAnyRole('ROLE_USER','ROLE_ADMIN')") 
      .and().formLogin().loginPage("/login").defaultSuccessUrl("/user") 
      .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
      .and().csrf() 
      .and().exceptionHandling().accessDeniedPage("/access_denied"); 
} 

何が問題になりますか?リクエストgetAuthentication()への呼び出しをトリガし、それはまさにそれが必要な方法を動作しています1 -

+0

私はそう思っていると仮定して、あなたがログインしたときにエントリがデータベースに作成されると主張しましたか?おそらく初めのユーザーのためだけに機能しますか? –

+0

私は別の問題が見つかりましたので投稿を編集しました。 –

+0

新しいウィンドウを開くことを定義しますか? CTRL + TまたはCTRL + Nを押すと、新しいブラウザではない既存のブラウザが表示されます。セッションの状態、キャッシュ、Cookieがコピーされます。あなたは実際には複数のプレーヤーではなく、複数のブラウザウィンドウで1人のユーザーと遊んでいます。私はそれがうまくいくとうまくいくと言うでしょう、あなたは間違った方法でそれをテストしています。 –

答えて

3

SecurityContextHolderあなたは現在のスレッド、これだけ現在ユーザーに関連付けられているセキュリティコンテキストにアクセスできます。

一方、すべてのアクティブなセッション(ログインしているすべてのユーザー)が必要な場合は、SessionRegistryを注入してsessionRegistry.getAllPrincipals()を呼び出す必要があります。

詳細はすでにhereです。

関連する問題