2

ユーザーが正常に登録された後、ログイン ページの後にユーザーをリダイレクトしようとしており、SecurityContextHolder.getContext().getAuthentication(); からアクティブ ユーザーを取得しようとしています。nullを返しています。

ユーザーが正常に登録されたときにコンテキストを設定する方法は次のとおりです

 UsernamePasswordAuthenticationToken authenticatedUser = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
 authenticatedUser.setDetails(userDetails);

 if(authenticatedUser.isAuthenticated()) {
        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
 }

次に、response.sendRedirect(url); を使用してユーザーを x ページにリダイレクトしています。

コントローラーメソッドでは、現在のユーザーを次のようにしようとしています

@RequestMapping("/<pattern>")
public @ResponseBody <method_name>(HttpServletRequest req,HttpServletResponse resp){
     Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth==null){
            System.out.println("this is null");
        }
        else{
          // do something
        }
}

コンソールに「this is null」というテキストが表示されます。

ユーザー期間中、ユーザーがログアウトするまで SecurityContext() から現在のユーザー/プリンシパルを保持する方法。

4

1 に答える 1

0

私は解決策を得ました。

問題は、その特定の URI グループのタグのパターン属性にありました。

以前は /path/* で、失敗していました。/path/** に変更したところ、うまくいきました。

誰かがこれら 2 つのパターン マッチング (/* と /**) について説明できると助かります。

于 2015-06-16T16:48:15.273 に答える