2011-06-21 38 views
2

我想用玻璃魚的自定義領域3.1如何在GlassFish 3.1中使用自定義領域?

我從這個主題的兩個文件嘗試。 Custom Glassfish Security Realm does not work (unable to find LoginModule)

的CustomRealm.java

package com.company.security.realm; 
import com.sun.appserv.security.AppservRealm; 
import com.sun.enterprise.security.auth.realm.BadRealmException; 
import com.sun.enterprise.security.auth.realm.InvalidOperationException; 
import com.sun.enterprise.security.auth.realm.NoSuchRealmException; 
import com.sun.enterprise.security.auth.realm.NoSuchUserException; 
import java.util.Enumeration; 
import java.util.Properties; 
import java.util.Vector; 

public class CustomRealm extends AppservRealm 
{ 
Vector<String> groups = new Vector<String>(); 

private String jaasCtxName; 

private String startWith; 

@Override 
public void init(Properties properties) 
throws BadRealmException, NoSuchRealmException { 
    jaasCtxName = properties.getProperty("jaas-context", "customRealm"); 
    startWith = properties.getProperty("startWith", "z"); 
    groups.add("dummy"); 
} 

@Override 
public String getAuthType() 
{ 
    return "Custom Realm"; 
} 

public String[] authenticate(String username, char[] password) 
{ 
    // if (isValidLogin(username, password)) 
    return (String[]) groups.toArray(); 
} 

@Override 
public Enumeration getGroupNames(String username) 
throws InvalidOperationException, NoSuchUserException 
{ 
    return groups.elements(); 
} 

@Override 
public String getJAASContext() 
{ 
    return jaasCtxName; 
} 

public String getStartWith() 
{ 
    return startWith; 
} 
} 

而且自定義登錄模塊

package com.company.security.realm; 

import com.sun.appserv.security.AppservPasswordLoginModule; 
import com.sun.enterprise.security.auth.login.common.LoginException; 
import java.util.Set; 
import org.glassfish.security.common.PrincipalImpl; 

public class CustomLoginModule extends AppservPasswordLoginModule 
{  
    @Override 
protected void authenticateUser() throws LoginException 
{ 
    _logger.info("CustomRealm : authenticateUser for " + _username); 
    final CustomRealm realm = (CustomRealm)_currentRealm; 

    if ((_username == null) || (_username.length() == 0) || !_username.startsWith(realm.getStartWith())) 
     throw new LoginException("Invalid credentials"); 

    String[] grpList = realm.authenticate(_username, getPasswordChar()); 
    if (grpList == null) { 
     throw new LoginException("User not in groups"); 
    } 

    _logger.info("CustomRealm : authenticateUser for " + _username); 

    Set principals = _subject.getPrincipals(); 
    principals.add(new PrincipalImpl(_username)); 

    this.commitUserAuthentication(grpList); 

} 
} 

我加爲好模塊conf文件

customRealm { 
com.company.security.realm.CustomLoginModule required; 
}; 

我複製我的2 .class in glassfish3/glassfish/domains/domain1/lib/classes/ 以及glassfish3/glassfish/lib

每當我想創建一個新領域時,我都遇到了同樣的錯誤。

./asadmin --port 4949 create-auth-realm --classname com.company.security.realm.CustomRealm --property jaas-context=customRealm:startWith=a customRealm  

remote failure: Creation of Authrealm customRealm failed. com.sun.enterprise.security.auth.realm.BadRealmException: java.lang.ClassNotFoundException: com.company.security.realm.CustomRealm not found by org.glassfish.security [101] 

com.sun.enterprise.security.auth.realm.BadRealmException: java.lang.ClassNotFoundException: com.company.security.realm.CustomRealm not found by org.glassfish.security [101] 
Command create-auth-realm failed. 

我想我真的不明白如何以正確的方式添加我的兩個文件到glassfish。

這兩個文件是從eclipse創建和編譯的。我創建了一個java項目suctom登錄。

有人可以提供幫助嗎?

THX在前進, LOIC

回答

0

你打包爲一個OSGi模塊(請在你所引用的職位的答案)?如果是這樣,不要複製jar文件到$ GF_HOME/lib或什麼,而不是將其部署爲一個OSGi模塊:

asadmin deploy --type osgi /path/to/CustomRealm.jar

然後加入login.conf的設置。爲了安全起見,我重新啓動GF(asadmin restart-domain),然後您可以使用您在那裏的命令創建領域。

+1

嗨,我試過了,唯一的問題是,當我將它部署爲OSGI包時,我的Jar從未找到...是否有適當的方式而不是使用完整的類路徑。 –