Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在自定义安全领域(Glassfish)中使用自定义主体?_Java_Security_Jakarta Ee_Glassfish - Fatal编程技术网

Java 如何在自定义安全领域(Glassfish)中使用自定义主体?

Java 如何在自定义安全领域(Glassfish)中使用自定义主体?,java,security,jakarta-ee,glassfish,Java,Security,Jakarta Ee,Glassfish,我遵循为我的glassfish创建自定义安全域的方法。这一切都很好,用户的身份验证是正确的。然而,问题如下: 用户凭据以字符串形式加密 领域解密此字符串并对数据库执行身份验证(works) 与在securityContext中将解密的值用作主体不同,加密的 字符串被传递 我已经尝试重写commit()方法来替换\u userPrincipal,或者使用getSubject().getPrincipals().add(new PrincipalImpl(“用户”)附加我自己的实现。两人都没有按

我遵循为我的glassfish创建自定义安全域的方法。这一切都很好,用户的身份验证是正确的。然而,问题如下:

  • 用户凭据以字符串形式加密
  • 领域解密此字符串并对数据库执行身份验证(works)
  • 与在securityContext中将解密的值用作主体不同,加密的 字符串被传递
我已经尝试重写commit()方法来替换
\u userPrincipal
,或者使用
getSubject().getPrincipals().add(new PrincipalImpl(“用户”)
附加我自己的实现。两人都没有按预期工作。基本上,问题很简单:如何在glassfish中的自定义安全领域中设置自己的主体,使其能够与注入的securityContext一起使用

我的环境:

  • Glassfish 3.1.2.2(构建5)完整剖面
  • 在身份验证之后运行的应用程序是基于JAX-RS1.1的应用程序
  • SecurityContext是通过注入获得的
我已经尝试重写commit()方法来替换 _userPrincipal或使用getSubject().getPrincipals().add(新PrincipalImpl(“用户”))附加我自己的实现。也不 一切正常

您会遇到什么样的错误

无论如何,我认为你的问题在于这个过程的第三步。SecurityContext仅将基本身份验证、表单身份验证、客户端证书身份验证、摘要身份验证定义为AuthenticationScheme,因此SecurityContext可能无法看到您的安全方案或类型的实现。但是你可以试试这些步骤,我希望它们对你有用

A-实现Java身份验证和授权服务(JAAS)登录模块或扩展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.
}
实现你的领域类

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

为此,您需要了解JSR196,并通过实现javax.security.auth.message.module.ServerAuthModule编写您自己的SAM。看看下面的链接。

由于这个问题是10个月前提出的,我甚至不能再告诉你我们有什么bug了。不幸的是,我也无法测试您的方法,因为我们使用了不同的解决方案。但既然你努力想回答这样一个老生常谈的问题,我还是会投票支持你的答案:)