【问题标题】:How to use a Custom Principal in a custom security realm (Glassfish)?如何在自定义安全领域(Glassfish)中使用自定义主体?
【发布时间】:2013-04-02 16:14:29
【问题描述】:

我按照instructions 为我的 glassfish 创建了一个自定义安全领域。一切正常,用户已正确验证。但是问题如下:

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

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

我的环境:

  • Glassfish 3.1.2.2(内部版本 5)完整配置文件
  • 在身份验证后运行的应用程序是基于 JAX-RS 1.1 的应用程序
  • SecurityContext 是通过注入获得的

【问题讨论】:

    标签: java security jakarta-ee glassfish


    【解决方案1】:

    我已经尝试覆盖 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

    【讨论】:

    • 自从 10 个月前提出这个问题以来,我什至不能再告诉你我们有什么错误。不幸的是,我也无法测试您的方法,因为我们使用了不同的解决方案。但是,既然您努力尝试回答这么老的问题,我无论如何都会赞成您的回答:)
    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 2011-05-30
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多