源代码展示:
public String check(){
try{
ActionContext ac = ActionContext.getContext();
request =(HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
String ip = request.getRemoteAddr();
boolean isUse = checkIP(ip);
if(!isUse){
this.result = "4";
}else{
String tempStr = pubUserService.login(this.userName, this.password);
//(b +":"+ tempIsAdmin+":"+tempIFManager+":"+tempIfOpen)
String[] tempArr = tempStr.split(":");
if (tempArr[0].equals("false")) {
this.result = "1";//用户、密码不正确
}else if (!tempArr[3].equals("启用") && !Constants.IF_ADMIN.equals(tempArr[1].trim())) {
this.result = "3";//用户或角色处于禁用状态,不允许登录
// }else if(tempArr[0].trim().equals("true") && !Constants.IF_ADMIN.equals(tempArr[1].trim()) && !tempArr[2].equals("1")){
// this.result = "2";//不是系统管理管理不允许登录ESB
}else if(tempArr[0].trim().equals("true")){
this.result = "0";//超级管理员或系统管理员允许登录ESB
}
}
}catch(Exception e){
e.printStackTrace();
return "error";
}
return "success";
}
项目在登陆时遇到问题:
在用域名:localhost:8081/esb 访问时控制台会报错
数据格式错误:0:0:0:0:0:0:1
在网上搜了搜解决办法,自己总结一下:
就是计算机上C:\Windows\System32\drivers\etc下的hosts文件里:
原本配置是:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
更改之后的配置是:
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
这样就解决了,用localhost,与127.0.0.1登陆都可以。
原因是:
因为机器上启用的IPv6协议,所以在对localhost进DNS解析时,得到的是IPv6形式的本机地址0:0:0:0:0:0:0:1。因此在利用http://localhost:8080/demo/ 访问时,request.getRemoteAddr()函数才会得到上面的结果。
可以修改hosts文件。位置:C:\Windows\System32\drivers\etc。添加上一句127.0.0.1 localhost,这样request.getRemoteAddr()函数得到的将是127.0.0.1。
此外,利用别的机器来访问本机服务器,则不会出现上述上问题,能够取得它的IPv4地址。对于网上所说的因为tomcat的反向代理,使得request.getRemoteAddr()无法获得客户端真实IP的问题,我目前还不是很理解。
关于hosts文件
hosts文件相当于一个位于本地的IP地址到域名的映射文件,可以提供DNS解析。如果想访问的网站被域名污染(域名劫持),可以预先将IP,域名对写入hosts文件。