2016-06-28 75 views
0

我知道這個問題可以用主觀方式回答,但我想知道是否有最佳實踐來處理Spring Web應用程序中的認證用戶數據。在Spring Web應用程序中處理認證數據的最佳實踐

我的意思是:我需要在我的應用程序的服務層的幾個類中檢索經過身份驗證的用戶的用戶標識。

目前我得到在我的控制器方法自動裝配的Principal對象:

@RequestMapping("/some/path", method = RequestMethod.GET) 
public String getById(@RequestParam Integer id, Principal principal){ ... } 

,並在每個方法調用傳遞Principal.getName(),例如:

// service class method 
public void doSomething(Integer id, String username){ 

    // retrieve user by username from app DB 
} 

我知道我可以簡單地打電話:

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 

在我的服務l ayer來獲得當前認證的用戶,但我想知道這對我的應用程序是否是一個糟糕的設計。

處理驗證數據的常用方法是什麼?我應該在需要的地方找回它,還是應該通過我的應用程序的「關卡」?

+0

許多優秀的問題根據專家的經驗產生了一定程度的意見,但對這個問題的回答往往幾乎完全基於意見,而不是事實,參考或具體的專業知識。 –

+0

這樣做的一種方法是使用自定義身份驗證。您應該在用戶類中實現spring userdetails,並且在身份驗證之後,您可以在會話中將其設置並在您的方法中使用它 –

+0

自定義Spring身份驗證和授權將有助於解決此問題。認證後,UserDetailService可以檢索數據並將其存儲爲會話值。 Spring安全默認使用[密碼加密機制,這對於增強安全性非常有用。 – nijogeorgep

回答

0

您可以添加認證後處理程序作爲

1.登錄安全XML

\t 
 
<form-login login-page="/login.jsp" 
 
    \t \t \t authentication-success-handler-ref="authenticationSuccessHandler" 
 
    \t \t \t authentication-failure-url="/login.jsp?error=true" /> 
 
    \t \t <logout logout-success-url="/login.jsp" />  \t \t 
 
    \t </http> 
 
    \t <beans:bean id="authenticationSuccessHandler" class="com.login.AuthenticationSuccessHandler"> 
 
    \t \t <beans:property name="defaultTargetUrl" value="/dashboard" /> 
 
    \t </beans:bean>

2. Java的類

 package com.login; 
 
     import java.io.IOException; 
 
     import javax.servlet.ServletException; 
 
     import javax.servlet.http.HttpServletRequest; 
 
     import javax.servlet.http.HttpServletResponse; 
 
     import javax.servlet.http.HttpSession; 
 
     
 
     import org.springframework.beans.factory.annotation.Autowired; 
 
     import org.springframework.security.core.Authentication; 
 
     import org.springframework.security.core.context.SecurityContextHolder; 
 
     import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 
 
     
 
     import com.commonFunction.CommonFunction; 
 
     
 
     public class AuthenticationSuccessHandler extends 
 
     SavedRequestAwareAuthenticationSuccessHandler { 
 
     \t @Autowired 
 
     \t CommonFunction commonFunction; 
 
     \t /** 
 
     \t * Override Method to set session after 
 
     \t * successfully Authentication 
 
     \t * By Prince 5/5/16 
 
     \t */ 
 
     \t @Override 
 
     \t public void onAuthenticationSuccess(HttpServletRequest request, 
 
     \t \t \t HttpServletResponse response, Authentication authentication) 
 
     \t \t \t \t \t throws IOException, ServletException { 
 
     \t \t super.onAuthenticationSuccess(request, response, authentication); 
 
     \t \t HttpSession session = request.getSession(true); 
 
     
 
     \t \t try { 
 
     \t \t \t Authentication auth=SecurityContextHolder.getContext().getAuthentication(); 
 
     \t \t \t LoginBean login=(LoginBean)auth.getPrincipal(); 
 
     \t \t \t session.setAttribute("vendorId",login.getVendorId()); 
 
     \t \t \t session.setAttribute("loginId",login.getUsername()); 
 
     \t \t \t session.setAttribute("userName",login.getVendorName()); 
 
     \t \t \t session.setAttribute("vendorType",login.getVendorType()); 
 
     \t \t \t session.setAttribute("navigationList",commonFunction.getNavigationContent(request)); 
 
     \t \t \t 
 
     \t \t \t System.out.println("Successfully Authenticate:"+login.getVendorId()); 
 
     \t \t } catch (Exception e) { 
 
     \t \t \t logger.error("Error in getting User()", e); 
 
     \t \t \t e.printStackTrace(); 
 
     \t \t } 
 
     \t } 
 
     
 
     }

相關問題