2017-05-25 167 views
0

Spring安全性提供了保護頁面和重定向用戶訪問被拒絕頁面的好方法。我期望使用spring security來做的事情是,如果用戶試圖訪問頁面A並且他無權訪問,我希望用戶留在同一頁面上查看所有相關的菜單項(菜單項提供瞭如果他們購買該權利,他們會得到什麼樣的想法),同時我想告訴用戶訪問被拒絕,他們需要購買該頻道。Spring Security授權:保護webapge的部分

我大概可以通過<security:authorize標籤來做到這一點,但由於我的應用程序中的內容組織方式,我想在url級別上做到這一點。

回答

0

我們做了一些類似的爲我們的產品

  1. 在安全性方面,RememberMeAuthenticationToken使用彈簧庫中,以便用戶的創建類型的認證匿名/記得,我的類型
Authentication authentication = rememberMeServices.createSuccessfulAuthentication(request, 
    userDetails); 

2.創建這些API在JSP頁面中使用

public static boolean isFullyAuthenticated() { 
    SecurityContext securityContext = SecurityContextHolder.getContext(); 
    if (securityContext != null) { 
    Authentication authentication = securityContext.getAuthentication(); 
    return isFullyAuthenticated(authentication); 
    } 
    return false; 

} 

public static boolean isFullyAuthenticated(Authentication authentication) { 
    return !authenticationTrustResolver.isAnonymous(authentication) && !authenticationTrustResolver 
    .isRememberMe(authentication); 
} 

其中authenticationTrustResolver由Spring提供 AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl();

  • 在JSP中添加必要的檢查
  • <%if(isFullyAuthenticated()){%> 
     
        //Menu option with links 
     
        <%}else{%> 
     
    //menu option which shows popup/ msgs 
     
        <%}%>

    注:該實用程序方法明確寫出,因爲我們沒能找到rememberme /匿名用戶的安全標籤

    +0

    感謝Amirutha,我認爲類似的解決方案可以使用spring security提供的選項卡庫來實現,但它也是開發人員可以使用的東西爲了使其發揮作用,我從框架的角度來看待更多,如果有一個「正確」的方法來實現這一點,那就是Spring Security推薦的方法。 –

    0

    我認爲你應該這樣做,實施自定義org.springframework.security.web.access.AccessDeniedHandler。每當用戶嘗試訪問無權訪問的頁面時,該頁面將被拋出。在handle方法中,您收到的參數爲HttpServletRequest,您可以使用該方法對每個請求端點進行一種特定的處理。

    @Override 
        public void handle(HttpServletRequest request, HttpServletResponse response, 
         AccessDeniedException accessDeniedException) 
           throws IOException, ServletException { 
    
         String errorPage = doSomethingWithRequestUri(request); 
         response.sendRedirect(errorPage); 
    
        } 
    

    但是你必須非常小心,這樣如果您嘗試重定向到相同的URL,用戶試圖進入你會愛上一個無限循環重定向。也許你應該使用授權/非授權模式,比如這個:

    • /授權/動作/一
    • /授權/動作/一

    你可能已經想到了這一點,你已經實現瞭解決方案...因此,在AccessDeniedHandler實現中,您應該注意授權/未授權的url關係

    相關問題