我遵循instructions为我的glassfish创建了一个自定义安全领域。一切正常,对用户进行了正确的身份验证。但是,问题如下:

  • 用户凭证以字符串
  • 加密
  • 领域解密此字符串并针对数据库执行验证(有效)
  • 而不是将已解密的值用作securityContext中的主体,而是使用已加密的值。
    字符串被传递。

  • 我已经尝试覆盖commit()方法来替换_userPrincipal或使用getSubject().getPrincipals().add(new PrincipalImpl("user"))附加我自己的实现。两者均未按预期工作。基本上,这个问题很简单:我如何在Glassfish的自定义安全领域中设置自己的主体,使其可以与注入(inject)的securityContext一起使用?

    我的环境:
  • Glassfish 3.1.2.2(内部版本5)完整文件
  • 身份验证后运行的应用程序是基于JAX-RS 1.1的应用程序
  • 使用注入(inject)
  • 获得SecurityContext。

    最佳答案



    您会遇到哪种错误?

    无论如何,我认为您的问题在于此过程的第三步。 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-实现您的领域类(class)。
    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

    10-01 07:47
    查看更多