2015-11-08 19 views
6

春のセキュリティバージョンを非推奨しました。春のセキュリティAuthenticatedPricipalは

同じことを実現する異なる方法があり、これがSpring MVCコントローラ内でユーザー情報を取得する正しい方法であるかどうかは疑問です。別のノートで

@RequestMapping(method = RequestMethod.GET) 
public String getIndex(HttpSession session, Device device, Model model, Principal principal) { 

    /** 
    * Spring Security Fetch User 
    */ 
    if (principal != null) { 
     String username = principal.getName(); 
     User currentUser = userRepository.findByEmail(username); 
     model.addAttribute("user", currentUser.getFirstName()); 
    } 

    return "view"; 

} 

私はHttpSession sessionは、ページの読み込みエラーが発生古いセッションにつながる持っていない見つけました。

答えて

15

作成することによって、呼び出しuserRepostiory(データベース、重複コール)を避けることができます動作するはずです。

推奨されないorg.springframework.security.web.bind.annotation.AuthenticationPrincipalの代わりにorg.springframework.security.core.annotation.AuthenticationPrincipalを使用してください。

-1

以下のコードスニペットは、

@RequestMapping(method = RequestMethod.GET) 
public String getIndex(HttpSession session, Device device, Model model) { 

    /** 
    * Spring Security Fetch User 
    */ 

    User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    String username = user.getUsername(); 
    User currentUser = userRepository.findByEmail(username); 
    model.addAttribute("user", currentUser.getFirstName()); 

    return "view"; 

} 

あなたはユーザークラスを拡張し、注釈が別のパッケージに移動されたフィールドfirstNameの

関連する問題