簡體   English   中英

在 Java Swing 應用程序中實現 Spring Security

[英]Implement Spring Security in Java Swing application

你們中有人清楚我如何實施這個過程嗎? 我知道我們需要一個 XML,我們可以在其中對數據庫進行查詢,如下所示:

<security:authentication-manager>
        <security:authentication-provider >
            <security:jdbc-user-service data-source-ref="dataSource"
              users-by-username-query="
              select  emailid username,password,'true' enabled from tbl_LoginDetails
              where emailid=?"
             authorities-by-username-query="
             select a.emailid username,b.authority from tbl_LoginDetails a,tbl_UserRoles b
            where a.userId=b.userId
            and a.emailid=?"/>
            <security:password-encoder  ref="passwordEncoder">
            </security:password-encoder>
        </security:authentication-provider>
    </security:authentication-manager>
<bean name="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">

但是,我還需要從 Java Swing 應用程序中做些什么。 我的意思是:我有一個帶有兩個文本框的窗口,用於輸入用戶和密碼。 我還需要做什么?

如果要在 Swing 應用程序中使用 Spring Security,則需要在SecurityContextHolder使用GlobalSecurityContextHolderStrategy

SecurityContextHolderStrategy 的基於靜態字段的實現。 這意味着 JVM 中的所有實例共享相同的 SecurityContext。 這對於富客戶端通常很有用,例如Swing

查看SecurityContextHolder Java Docs 以了解如何配置它:

將給定的 SecurityContext 與當前執行線程相關聯。 此類提供了一系列委托給 SecurityContextHolderStrategy 實例的靜態方法。 該類的目的是提供一種方便的方法來指定應該用於給定 JVM 的策略。 這是 JVM 范圍的設置,因為此類中的所有內容都是靜態的,以方便調用代碼時的使用。 要指定應使用哪種策略,您必須提供模式設置。 模式設置是定義為靜態最終字段的三個有效 MODE_ 設置之一,或者是提供公共無參數構造函數的 SecurityContextHolderStrategy 的具體實現的完全限定類名。 有兩種方法可以指定所需的策略模式字符串。 第一種是通過以 SYSTEM_PROPERTY 為鍵的系統屬性來指定它。 第二種是在使用類之前調用​​ setStrategyName(String) 。 如果這兩種方法都沒有使用,該類將默認使用 MODE_THREADLOCAL,它向后兼容,具有較少的 JVM 不兼容性並且適用於服務器(而 MODE_GLOBAL 絕對不適合服務器使用)。

您不想在獨立的 Swing 應用程序中使用 spring 框架。 對於上述身份驗證,直接 JDBC 查詢要簡單得多、輕量級、直接,並且通常是桌面 RCP 應用程序中的方法。 它只是幾行 Java 代碼,可能比您的 XML 少。 如果您需要的不僅僅是檢查用戶名/密碼,請考慮 RCP 領域中的高級框架。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM