Commit 5d8d38ae by 宋振民

feat:mac地址登录控制

parent a820efe6
......@@ -14,6 +14,7 @@ class HsAdminApplicationTests {
@Test
void contextLoads() {
}
}
package com.hs.api.common.enums;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public enum SysConfigKeyType {
MAC_ADDRESS_RANGE("MAC_ADDRESS_RANGE","MAC地址范围过滤"),
MAC_BINDING("MAC_BINDING","MAC地址绑定");
private String value;
private String desc;
SysConfigKeyType(String value, String desc) {
this.value = value;
this.desc = desc;
}
public String getValue() {
return value;
}
public String getDesc() {
return desc;
}
public static List<Map<String,Object>> all() {
List<Map<String,Object>> list = new ArrayList<>();
for(SysConfigKeyType projectType : values()){
Map<String,Object> map = new HashMap<String,Object>() {
{
put("value",projectType.getValue());
put("description",projectType.getDesc());
}
};
list.add(map);
}
return list;
}
}
package com.hs.api.mapper;
import com.hs.api.model.SysConfig;
import com.hs.api.model.SysUser;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface SysLoginConfigMapper {
List<SysConfig> selectByKey(String key);
}
\ No newline at end of file
package com.hs.api.model;
import java.util.Date;
public class SysConfig {
private Long id;
private Date createDate;
private boolean state;
private String key;
private String name;
private String value;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public boolean getState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
\ No newline at end of file
package com.hs.api.service.Impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hs.api.common.enums.SysConfigKeyType;
import com.hs.api.common.exceptions.DBConfigurationError;
import com.hs.api.common.utils.TokenUtil;
import com.hs.api.mapper.DicOrgMapper;
import com.hs.api.mapper.SysRoleMapper;
import com.hs.api.mapper.SysUserMapper;
import com.hs.api.mapper.SysVersionMapper;
import com.hs.api.mapper.*;
import com.hs.api.model.*;
import com.hs.api.model.respmodel.LoginInfo;
import com.hs.api.service.LoginService;
import com.hs.api.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
......@@ -32,6 +35,8 @@ public class LoginServiceImpl implements LoginService {
private SysRoleMapper sysRoleMapper;
@Resource
private SysVersionMapper sysVersionMapper;
@Resource
private SysLoginConfigMapper sysLoginConfigMapper;
@Override
public LoginInfo login(String logName, String password, String macAddrss, String ip) {
......@@ -39,7 +44,10 @@ public class LoginServiceImpl implements LoginService {
LoginInfo loginInfo = new LoginInfo();
SysUser user = userServiceImpl.findByLoginName(logName);
if (user != null && user.getPassword().equals(password) && (user.getMacAddress() == null || user.getMacAddress().equals(macAddrss))) {
if(!checkMacAddress(macAddrss,user)) throw new DBConfigurationError("当前MAC地址无法登录!");
if (user != null && user.getPassword().equals(password)) {
String token = TokenUtil.getToken(password, user);
//修改ip以及最后登录时间、MAC地址
user.setLastIp(ip);
......@@ -65,11 +73,48 @@ public class LoginServiceImpl implements LoginService {
SysVersion version = sysVersionMapper.selectLastDate();
loginInfo.setVersion(version);
} else {
throw new DBConfigurationError("账号、密码或者MAC地址错误!");
throw new DBConfigurationError("账号、密码错误!");
}
return loginInfo;
}
private boolean checkMacAddress(String macAddress, SysUser user) {
if(checkMacRange(macAddress) && checkMacBind(macAddress, user.getMacAddress())) {
return true;
}
return false;
}
private boolean checkMacRange(String macAddress) {
List<SysConfig> sysConfigList = sysLoginConfigMapper.selectByKey(SysConfigKeyType.MAC_ADDRESS_RANGE.getValue());
if(sysConfigList.size() == 0) return true;
SysConfig sysConfig = sysConfigList.get(0);
//mac地址范围控制
if(sysConfig.getState()) {
String optionalValue = sysConfig.getValue();
String[] macAddrArr = optionalValue.split(",");
for (String macAddr : macAddrArr) {
if(macAddr.equals(macAddress)) return true;
}
}else {
return true;
}
return false;
}
private boolean checkMacBind(String macAddress, String lastMacAddress) {
List<SysConfig> sysConfigList = sysLoginConfigMapper.selectByKey(SysConfigKeyType.MAC_BINDING.getValue());
if(sysConfigList.size() == 0) return true;
SysConfig sysConfig = sysConfigList.get(0);
if(sysConfig.getState()) {
if(lastMacAddress.equals("") || macAddress.equals(lastMacAddress)) return true;
}else {
return true;
}
return false;
}
public boolean updatePass(String logName, String oldPassword, String newPassword) {
int result = 0;
SysUser user = userServiceImpl.findByLoginName(logName);
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hs.api.mapper.SysLoginConfigMapper">
<resultMap id="ResultMap" type="com.hs.api.model.SysConfig">
<id column="ID" jdbcType="DECIMAL" property="id"/>
<result column="CREATE_DATE" jdbcType="TIMESTAMP" property="createDate"/>
<result column="STATE" jdbcType="BOOLEAN" property="state"/>
<result column="KEY" jdbcType="VARCHAR" property="key"/>
<result column="NAME" jdbcType="VARCHAR" property="name"/>
<result column="VALUE" jdbcType="VARCHAR" property="value"/>
</resultMap>
<select id="selectByKey" resultMap="ResultMap">
select *
from SYS_CONFIG
where `key` = #{key,jdbcType=VARCHAR}
</select>
</mapper>
\ No newline at end of file
......@@ -2,11 +2,17 @@ package com.hs.api;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootApplication
@MapperScan("com.hs.api.mapper")
@ComponentScan(basePackages = {"com.hs.common.config", "com.hs.api"})
public class BsoftApiApplicationTests {
@Test
......
package com.hs.api.service;
import com.hs.api.BsoftApiApplicationTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BsoftApiApplicationTests.class)
public class LoginServiceTest {
@Autowired
LoginService loginService;
@Test
public void test() {
String logName = "hospital";
String password = "123";
String macAddrss = "EO-D5-5E-6C-4D-7D";
String ip = "112.64.68.44";
loginService.login(logName, password, macAddrss, ip);
System.out.println(1111);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment