0

在我的系统中,我希望只有管理员帐户才能访问特定文件夹。

所以当客户端以自己的身份登录时,单击打开文件链接,我希望它临时切换到管理员帐户来打开文件。关闭打开文件后,账户恢复到客户的原始账户。

我发现它可以通过这个链接在 ASP.NET 中的 C# 中完成:

如何将登录用户更改为另一个用户?

想知道我们是否可以在 Java 中做到这一点?

4

2 回答 2

2

您可以通过 Runetim.exec() 调用 RUNAS 以其他用户身份执行任务

RUNAS USAGE:

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
        /user:<UserName> program

RUNAS [ [/noprofile | /profile] [/env] [/savecred] ]
        /smartcard [/user:<UserName>] program

RUNAS /trustlevel:<TrustLevel> program

   /noprofile        specifies that the user's profile should not be loaded.
                     This causes the application to load more quickly, but
                     can cause some applications to malfunction.
   /profile          specifies that the user's profile should be loaded.
                     This is the default.
   /env              to use current environment instead of user's.
   /netonly          use if the credentials specified are for remote
                     access only.
   /savecred         to use credentials previously saved by the user.
                     This option is not available on Windows 7 Home or Windows 7 Starter Editions
                     and will be ignored.
   /smartcard        use if the credentials are to be supplied from a
                     smartcard.
   /user             <UserName> should be in form USER@DOMAIN or DOMAIN\USER
   /showtrustlevels  displays the trust levels that can be used as arguments
                     to /trustlevel.
   /trustlevel       <Level> should be one of levels enumerated
                     in /showtrustlevels.
   program         command line for EXE.  See below for examples

Examples:
> runas /noprofile /user:mymachine\administrator cmd
> runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
> runas /env /user:user@domain.microsoft.com "notepad \"my file.txt\""

http://ss64.com/nt/runas.html

于 2012-11-05T17:09:18.103 回答
0

如果您使用的是 Java EE,我认为您可以使用 @RunAs 注释(但我没有尝试过): http ://docs.oracle.com/javaee/6/api/javax/annotation/security/RunAs.html

编辑:

这里是更多关于 JAAS“doAs”的链接,但我还没有研究过:http: //docs.oracle.com/javase/7/docs/technotes/guides/security/jaas/JAASRefGuide.html

于 2012-11-05T17:45:57.977 回答