program tip

자바에서 로그인 사용자 이름 가져 오기

radiobox 2020. 9. 20. 09:23
반응형

자바에서 로그인 사용자 이름 가져 오기


Java에서 사용자 이름 / 로그인 이름을 얻으려면 어떻게해야합니까?

이것은 내가 시도한 코드입니다 ...

try{
    LoginContext lc = new LoginContext(appName,new TextCallbackHandler());
    lc.login();
    Subject subject = lc.getSubject();
    Principal principals[] = (Principal[])subject.getPrincipals().toArray(new Principal[0]);

    for (int i=0; i<principals.length; i++) {
        if (principals[i] instanceof NTUserPrincipal || principals[i] instanceof UnixPrincipal) {
            String loggedInUserName = principals[i].getName();
        }
    }

}
catch(SecurityException se){
    System.out.println("SecurityException: " + se.getMessage());
}

SecurityException이 코드를 실행하려고 하면 오류가 발생합니다. 누군가 내가 올바른 방향으로 가고 있는지 알려주고 문제를 이해하도록 도와주세요.


System.getProperty("user.name")

유닉스에서 :

new com.sun.security.auth.module.UnixSystem().getUsername()

Windows에서 :

new com.sun.security.auth.module.NTSystem().getName()

Solaris :

new com.sun.security.auth.module.SolarisSystem().getUsername()

System.getProperty ( "user.name")는 환경 변수가 위조 될 수 있으므로 좋은 보안 옵션이 아닙니다. C : \ set USERNAME = "Joe Doe"java ... // System.getProperty ( "user. name ")해야 할 일 :

com.sun.security.auth.module.NTSystem NTSystem = new com.sun.security.auth.module.NTSystem();
System.out.println(NTSystem.getName());

JDK 1.5 이상.

애플릿 내에서 사용하고 서명해야합니다. 정보 출처


모든 플랫폼에서 컴파일 할 수있는 코드 인 @newacct 의 답변에서 영감을 얻었습니다 .

String osName = System.getProperty( "os.name" ).toLowerCase();
String className = null;
String methodName = "getUsername";

if( osName.contains( "windows" ) ){
    className = "com.sun.security.auth.module.NTSystem";
    methodName = "getName";
}
else if( osName.contains( "linux" ) ){
    className = "com.sun.security.auth.module.UnixSystem";
}
else if( osName.contains( "solaris" ) || osName.contains( "sunos" ) ){
    className = "com.sun.security.auth.module.SolarisSystem";
}

if( className != null ){
    Class<?> c = Class.forName( className );
    Method method = c.getDeclaredMethod( methodName );
    Object o = c.newInstance();
    System.out.println( method.invoke( o ) );
}

JNA를 사용하면 간단합니다.

String username = Advapi32Util.getUserName();
System.out.println(username);

Advapi32Util.Account account = Advapi32Util.getAccountByName(username);
System.out.println(account.accountType);
System.out.println(account.domain);
System.out.println(account.fqn);
System.out.println(account.name);
System.out.println(account.sidString);

https://github.com/java-native-access/jna


The 'set Username="Username" ' is a temporary override that only exists as long as the cmd windows is still up, once it is killed off, the variable loses value. So i think the

System.getProperty("user.name");

is still a short and precise code to use.


System.getenv().get("USERNAME"); - works on windows !

In environment properties you have the information you need about computer and host! I am saying again! Works on WINDOWS !

참고URL : https://stackoverflow.com/questions/797549/get-login-username-in-java

반응형