2016-06-17 58 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實現的),然後將請求發送到「默認靶網址」。

或者當有一個自定義成功處理程序時,成功處理程序將確定它的路徑。

相關問題