簡體   English   中英

如何在自定義安全領域(Glassfish)中使用自定義主體?

[英]How to use a Custom Principal in a custom security realm (Glassfish)?

我按照說明為我的glassfish創建了一個自定義安全領域。 一切正常,用戶已正確驗證。 但是,問題如下:

  • 用戶憑證以字符串加密
  • 領域解密該字符串並針對數據庫執行身份驗證(有效)
  • 傳遞加密的String而不是在securityContext中使用解密的值作為主體。

我已經嘗試重寫commit()方法來替換_userPrincipal或使用getSubject().getPrincipals().add(new PrincipalImpl("user"))附加我自己的實現。 兩者均未按預期工作。 基本上,這個問題很簡單:我如何在Glassfish的自定義安全領域中設置自己的主體,使其可以與注入的securityContext一起使用?

我的環境:

  • Glassfish 3.1.2.2(內部版本5)完整檔案
  • 身份驗證后運行的應用程序是基於JAX-RS 1.1的應用程序
  • 使用注入獲得SecurityContext

我已經嘗試重寫commit()方法來替換_userPrincipal或使用getSubject()。getPrincipals()。add(new PrincipalImpl(“ user”))附加我自己的實現。 兩者均未按預期工作。

您會遇到什么樣的錯誤?

無論如何,我認為您的問題在於此過程的第三步。 SecurityContext僅將BASIC_AUTH,FORM_AUTH,CLIENT_CERT_AUTH,DIGEST_AUTH定義為AuthenticationScheme,因此SecurityContext可能看不到您對安全方案或類型的實現。 但是您可以嘗試這些步驟,希望它們對您有用。

A-實現Java身份驗證和授權服務(JAAS)LoginModule或擴展com.sun.appserv.security.AppservPasswordLoginModule

public class MyLoginModule extends AppservPasswordLoginModule {

@Override
protected void authenticateUser() throws LoginException {
    if (!authenticate(_username, _password)) {
//Login fails
        throw new LoginException("LoginFailed");
    }
    String[] myGroups = getGroupNames(_username);
    commitUserAuthentication(myGroups);
}

private boolean authenticate(String username, String password) {
    /*
     Check the credentials against the authentication source, return true if          authenticated, return false otherwise
     */
    return true;
}

private String[] getGroupNames(String username) {
// Return the list of groups this user belongs to.
}

B-實現您的領域課程。

public class MyRealm extends AppservRealm {

@Override
public void init(Properties props)
throws BadRealmException, NoSuchRealmException {
//here you initialize the realm
}
@Override
public String getAuthType() {
return "Custom Realm";
}
}

C-將領域和LoginModule安裝並配置到服務器中。

為此,您需要查看JSR 196並通過實現javax.security.auth.message.module.ServerAuthModule編寫自己的SAM。 看看下面的鏈接。 https://blogs.oracle.com/enterprisetechtips/entry/adding_authentication_mechanisms_to_the

暫無
暫無

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

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