gpt4 book ai didi

java - tinyradius 验证方法导致 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 09:42:46 25 4
gpt4 key购买 nike

正如您从标题中可以想象的那样,我想使用 TinyRadius java 库与 Radius 服务器进行通信。我知道Radius服务器的目的是检查是否允许用户进入或使用某个网络。但我不需要授予某人访问新作品的权限,我只是想知道该用户是否被允许。

但是如果我尝试这样做,我就会遇到 NullPointerException,老实说我不知道​​为什么?首先是我的代码:

public static void main (String[] args) throws IOException{

String user = null;
String password = null;
ServerSocket listener = new ServerSocket(9090);

try{
while(true){
Socket socket = listener.accept();
try{
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(user == null || password == null){
user = input.readLine();
password = input.readLine();
}

String host = "AddressOfRadiusServer";
String shared = "SharedSecretIDontWantToShow";
String newUser = user;
String newPass = password;

//Shows that password and user are defenitely not null
System.out.println(user);
System.out.println(password);


RadiusClient rc = new RadiusClient(host, shared);
System.out.println("Packet sent");

//This is the Line where the Exception is thrown
if (rc.authenticate(user, password)) {
out.println("Result: Successfull");
out.println("CLOSE");
} else {
out.println("Result: Failure");
out.println("CLOSE");

}

rc.close();
user = null;
password = null;
}catch(Exception e){
e.printStackTrace();
}
finally{
socket.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
finally{
listener.close();
}

}

这是异常(exception):

Exception in thread "main" java.lang.ExceptionInInitializerError
at org.tinyradius.packet.RadiusPacket.<init>(RadiusPacket.java:998)
at org.tinyradius.packet.RadiusPacket.<init>(RadiusPacket.java:90)
at org.tinyradius.packet.AccessRequest.<init>(AccessRequest.java:50)
at org.tinyradius.util.RadiusClient.authenticate(RadiusClient.java:64)
at haw.landshut.ServerFinal.main(ServerFinal.java:43)`

Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at org.tinyradius.dictionary.DictionaryParser.parseDictionary(DictionaryParser.java:53)
at org.tinyradius.dictionary.DefaultDictionary.<clinit>(DefaultDictionary.java:48)`

我不确定为什么会发生这种情况,因为密码和用户都不为空。所以我不知道这里出了什么问题。

编辑:所以我想我稍微缩小了范围。

发生这样的事情:https://examples.javacodegeeks.com/java-basics/exceptions/java-lang-exceptionininitializererror-how-to-handle-exception-initializer-error/

在tinyradius库的这个类中:https://github.com/ctran/TinyRadius/blob/master/src/main/java/org/tinyradius/dictionary/DefaultDictionary.java

我不知道如何解决它。如果我改变它会起作用

private static DefaultDictionary instance = null;

private static DefaultDictionary instance = new DefaultDirecory();

预先感谢您的帮助!

最佳答案

您需要将default_dictionary文件添加到tinyradius.jar中。

试试这个,从目标/类目录:

jar uf ../../target/tinyradius-1.0.2.jar org/tinyradius/dictionary/default_dictionary

或者更新pom.xml:

<build>
...
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>org/tinyradius/dictionary/default_dictionary</include>
</includes>
</resource>
</resources>
</build>

并重新运行 mvn。

关于java - tinyradius 验证方法导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com