1.登录时验证IP地址,保存/验证mac地址

2.用户管理,保存IP地址
master
xiaoning 1 year ago
parent 4b11d2a3ee
commit 10a309edd8

@ -34,5 +34,9 @@ public class Syuserp {
private BigDecimal YLZD5;
private BigDecimal YLZD6;
private String SJBS;
@ApiModelProperty(value="局域网ip地址")
private String ipAddress;
@ApiModelProperty(value="mac地址")
private String macAddress;
}

@ -1,5 +1,6 @@
package com.dsic.gj_erp.controller;
import cn.hutool.core.util.StrUtil;
import com.dsic.gj_erp.annotation.AuthFunction;
import com.dsic.gj_erp.bean.ResultBean;
import com.dsic.gj_erp.bean.request.LoginBean;
@ -35,7 +36,8 @@ public class ApiController {
@Autowired
SYService syService;
@RequestMapping(value = "/login")
public ResultBean login(@RequestBody @Validated LoginBean loginBean) throws Exception {
public ResultBean login(@RequestBody @Validated LoginBean loginBean,HttpServletRequest request) throws Exception {
// List<String> list=new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// String a= syService.f_getpjh("DM","DM","CZPL");
@ -47,7 +49,7 @@ public class ApiController {
// String hostAddress = InetAddress.getLocalHost().getHostAddress();
// System.out.print("hostAddress ");
// System.out.println(hostAddress);
return new ResultBean(apiService.userLogin(loginBean));
return new ResultBean(apiService.userLogin(loginBean, request));
}
@AuthFunction
@ -56,9 +58,7 @@ public class ApiController {
String yhdm = (String) request.getAttribute("yhdm");
String zxtdm = (String) request.getAttribute("zxtdm");
;
System.out.print("IpAddr ");
System.out.println(IPUtils.getIpAddr(request));
Map<String, Object> map= apiService.getInfo(yhdm);
map.put("menu", syuserMapper.getUserMenu(yhdm, zxtdm));

@ -13,6 +13,7 @@ import java.util.Map;
public interface SyuserMapper {
int updateUser (Syuserp syuserp);
int updateUserMac (Syuserp syuserp);
List<Map> getUserMenu(String usercode, String systemcode);
List<Map> getSystemList();

@ -1,5 +1,6 @@
package com.dsic.gj_erp.service;
import cn.hutool.core.util.StrUtil;
import com.dsic.gj_erp.bean.request.LoginBean;
import com.dsic.gj_erp.bean.sy.QSyfunp;
import com.dsic.gj_erp.bean.sy.QSyifrp;
@ -8,6 +9,7 @@ import com.dsic.gj_erp.bean.sy.Syuserp;
import com.dsic.gj_erp.dao.sy.SyuserpRepository;
import com.dsic.gj_erp.exception.CustomException;
import com.dsic.gj_erp.mapper.SyuserMapper;
import com.dsic.gj_erp.util.IPUtils;
import com.dsic.gj_erp.util.JwtUtil;
import com.dsic.gj_erp.util.MD5Util;
import com.querydsl.jpa.impl.JPAQueryFactory;
@ -17,6 +19,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@ -49,8 +52,11 @@ public class ApiService {
* @return
* @throws CustomException
*/
public Map<String, Object> userLogin(LoginBean loginBean) throws CustomException {
public Map<String, Object> userLogin(LoginBean loginBean, HttpServletRequest request) throws CustomException {
String pwd="";
String ip= IPUtils.getIpAddr(request);
String macAddress=IPUtils.getMACAddress(ip);
if (!StringUtils.isEmpty(loginBean.getPwd())){
pwd= MD5Util.encrypt(loginBean.getPwd());
}
@ -59,6 +65,18 @@ public class ApiService {
if (syuserp == null) {
throw new CustomException(10201, "登陆失败,账号密码错误!");
}
if (!syuserp.getIpAddress().equals(ip) ) {
throw new CustomException(10201, "登陆失败IP地址未授权");
}
if (StrUtil.isNotEmpty(syuserp.getMacAddress())&& !syuserp.getMacAddress().equals(macAddress)) {
throw new CustomException(10201, "登陆失败mac地址错误");
}
if (StrUtil.isEmpty(syuserp.getMacAddress())){
syuserp.setMacAddress(macAddress);
syuserMapper.updateUserMac(syuserp);
}
syuserp.setPWD(null);
String token = jwtUtil.generateToken(loginBean.getYhdm(), loginBean.getZxtbm(),syuserp.getYHMS());

@ -5,8 +5,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Enumeration;
/**
/**
* IP
*
* @Author xhj
@ -53,5 +58,86 @@ public class IPUtils {
return ip;
}
}
public static String getMACAddress(String ip) {
String str = "";
String macAddress = "";
// final String LOOPBACK_ADDRESS = "127.0.0.1";
final String LOOPBACK_ADDRESS = getLocalIP();
// final String LOOPBACK_ADDRESS = InetAddress.getLocalHost().getHostAddress();
// final String LOOPBACK_ADDRESS_ipv6="0:0:0:0:0:0:0:1";
// 如果为127.0.0.1,则获取本地MAC地址。
if (LOOPBACK_ADDRESS.equals(ip)) {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
// 貌似此方法需要JDK1.6。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
.getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
macAddress = sb.toString().trim().toUpperCase();
}catch (Exception e){
e.printStackTrace();
}
return macAddress;
} else {
// 获取非本地IP的MAC地址
try {
// System.out.println(ip);
Process p = Runtime.getRuntime()
.exec("nbtstat -A " + ip);
System.out.println("===process=="+p);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(ir);
while ((str = br.readLine()) != null) {
if(str.indexOf("MAC")>1){
macAddress = str.substring(str.indexOf("MAC")+9, str.length());
macAddress = macAddress.trim();
System.out.println("macAddress:" + macAddress);
break;
}
}
p.destroy();
br.close();
ir.close();
} catch (IOException ex) {
}
return macAddress;
}
}
public static String getLocalIP(){
String localip="";
try {
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface nif = nifs.nextElement();
// 获得与该网络接口绑定的 IP 地址,一般只有一个
Enumeration<InetAddress> addresses = nif.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (addr instanceof Inet4Address && nif.getName().equals("wlan1")) { // 只关心 IPv4 地址
System.out.println("网卡接口名称:" + nif.getName());
System.out.println("网卡接口地址:" + addr.getHostAddress());
localip=addr.getHostAddress();
}
}
}
}catch (Exception e){}
return localip;
}
}

@ -6,8 +6,13 @@
<update id="updateUser" parameterType="com.dsic.gj_erp.bean.sy.Syuserp">
update syuserp set yhms=#{yhms}, bmbm=#{bmbm},ylzd1=#{ylzd1},ylzd2=#{ylzd2},
where yhdm=#{yhdm}
update syuserp set yhms=#{YHMS}, bmbm=#{BMBM},ylzd1=#{YLZD1},ylzd2=#{YLZD2},
ip_address=#{ipAddress}
where yhdm=#{YHDM}
</update>
<update id="updateUserMac" parameterType="com.dsic.gj_erp.bean.sy.Syuserp">
update syuserp set mac_address=#{macAddress}
where yhdm=#{YHDM}
</update>
<select id="getUserMenu" resultType="Map">
SELECT distinct right(syfunp.ylzd2,2) as id,null as parent_id,syfunp.zxtbm as system_code,

Loading…
Cancel
Save