2016-06-17 19 views
0

ユーザがspring securityを使用して認証されるユーザログインインターフェイスを作成しました。Spring MVC認証の成功ハンドラとコントローラ

AuthenticationSuccessHandlerは、ユーザーを新しいページにリダイレクトしました。

また、ログインしたユーザーの名前を取得し、間違った資格情報のエラーメッセージを表示するために、loginControllerを実装したいと考えています。ここに私のHandlerコードは次のとおりです。

public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { 
protected final Log logger = LogFactory.getLog(this.getClass()); 

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 

protected MySimpleUrlAuthenticationSuccessHandler() { 
    super(); 
} 

@Override 
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException { 
    handle(request, response, authentication); 
    clearAuthenticationAttributes(request); 
} 

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException { 
    final String targetUrl = determineTargetUrl(authentication); 

    if (response.isCommitted()) { 
     logger.debug("Response has already been committed. Unable to redirect to " + targetUrl); 
     return; 
    } 

    redirectStrategy.sendRedirect(request, response, targetUrl); 
} 

protected String determineTargetUrl(final Authentication authentication) { 
    boolean isUser = false; 
    boolean isAdmin = false; 
    final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); 
    for (final GrantedAuthority grantedAuthority : authorities) { 
     if (grantedAuthority.getAuthority().equals("ROLE_USER")) { 
      isUser = true; 
      break; 
     } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) { 
      isAdmin = true; 
      break; 
     } 
    } 

    if (isUser) { 
     return "/static_htm.html"; 
    } else if (isAdmin) { 
     return "/console.html"; 
    } else { 
     throw new IllegalStateException(); 
    } 
} 

そして、私のcontrollerコード:

@Controller 
public class HelloController { 

@RequestMapping(value="/login", method = RequestMethod.GET) 
public String printWelcome(ModelMap model, Principal principal) { 

    String name = principal.getName(); 
    model.addAttribute("username", name); 
    model.addAttribute("message", "Spring Security Hello World"); 
    return "static_htm";     //page after successful login 

} 

@RequestMapping(value="/login", method = RequestMethod.GET) 
public String login(ModelMap model) { 

    return "login";      //login page 

} 

@RequestMapping(value="/loginfailed", method = RequestMethod.GET) 
public String loginerror(ModelMap model) { 

    //String errormessage = resources.getMessage("login.error", null, null); 
    model.addAttribute("error", "true"); 
    return "login";      //login page 

} 

} 

ハンドラが正常に動作しますが、私は、ユーザー名だけでなく、エラーメッセージを取得することはできませんよ。 handlercontrollerを一緒に使用するにはどうすればよいですか?

ご意見をいただければ幸いです。

答えて

0

あなたの質問から、あなたはログインコントローラでユーザー名を取得したいと思っています。

そうでない場合は、私の答えを無視してください。

実際には逆戻りしているかもしれません。

成功ハンドラは、「default-target-url」のカスタム実装と多少似ています。だから、それは実際にログインコントローラの後に実行され

...

ログインが成功すると、その後、要求は「デフォルト - 標的 - に送信されますがない以前に要求されたパスは、(これはSavedRequestAwareAuthenticationSuccessHandlerによって実装されている)があります場合にはurl "

カスタムハンドラハンドラがある場合、成功ハンドラはそのハンドラが移動するパスを決定します。

関連する問題