本次目标
实现后台登录鉴权功能。
接口文档
1.后台管理员登录
页面访问地址:http://admin/pages/main/login.html
2.接口描述
js请求地址 | http://{ip:port}/user/adminLogin |
---|---|
后台接收 | user/adminLogin |
请求方式 | POST |
请求参数 | String username;用户名 String psw;密码 String valicaod;验证码 |
返回数据 | 返回SysResult对象的json,其结构: Integer status; 200表示成功,其他表示失败。 String msg;成功返回 “ok”,失败返回其他信息。 Object data;根据需求携带其他数据。 |
数据库用户权限相关表设计
用户角色表
CREATE TABLE `tsb_role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '父级角色ID',
`role_name` varchar(64) NOT NULL COMMENT '角色名称',
`gen_time` bigint(20) DEFAULT NULL COMMENT '创建时间',
`description` varchar(200) DEFAULT NULL COMMENT '角色描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
用户角色关联表
CREATE TABLE `tsb_common_user_role_relation` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '记录标识',
`uid` bigint(20) NOT NULL COMMENT '用户id',
`rid` bigint(20) NOT NULL COMMENT '角色id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
权限表
CREATE TABLE `tsb_right` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) NOT NULL COMMENT '父权限',
`right_name` varchar(200) NOT NULL COMMENT '权限名称',
`description` varchar(200) DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
角色权限关联表
CREATE TABLE `tsb_role_right_relation` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_id` bigint(20) NOT NULL COMMENT '角色id',
`right_id` bigint(20) NOT NULL COMMENT '权限id',
`right_type` tinyint(4) NOT NULL COMMENT '(0:无权访问,1:有权访问)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
代码实现
控制层
package liwen.zhao;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@MapperScan("liwen.zhao.mappers")
@EnableEurekaClient
public class JDUserMicroserviceStarter {
public static void main(String[] args) {
SpringApplication.run(JDUserMicroserviceStarter.class,args);
}
}
业务层
接口类:
package liwen.zhao.service;
import liwen.zhao.common.jd.pojo.CommonUser;
import liwen.zhao.common.jd.vo.SysResult;
public interface UserService {
CommonUser findCommonUserByName(String userName);
SysResult adminLogin(String userName, String psw, String action);
}
实现类:
package liwen.zhao.service;
import liwen.zhao.common.jd.pojo.CommonUser;
import liwen.zhao.common.jd.pojo.Right;
import liwen.zhao.common.jd.pojo.Role;
import liwen.zhao.common.jd.utils.MD5Util;
import liwen.zhao.common.jd.vo.SysResult;
import liwen.zhao.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Value("${solt}")
private String solt;
@Autowired
UserMapper userMapper;
@Override
public CommonUser findCommonUserByName(String userName) {
return userMapper.findCommonUserByName(userName,"user/adminLogin");
}
@Override
public SysResult adminLogin(String userName, String psw, String action) {
//密码加盐并md5
psw= MD5Util.md5(psw+solt);
CommonUser commonUser;
try{
commonUser=userMapper.findCommonUserByNameAndPswAndAction(userName,psw,action);
}catch (Exception e){
e.printStackTrace();
return SysResult.build(500,"登录异常",null);
}
if(commonUser==null){
return SysResult.build(500,"用户名或密码错误",null);
}
//判断该用户具有后台登录权限
boolean ifOk=false;
List<Role> roles=commonUser.getRoles();
if(null!=roles&&roles.size()>0){
for(Role role:roles){
if(ifOk){
break;
}
List<Right> rights=role.getRightList();
if(null!=rights&&rights.size()>0){
for(Right right:rights){
if(1==right.getRight_type()){
ifOk=true;
break;
}
}
}
}
}
if(!ifOk){
return SysResult.build(500,"无权访问!",null);
}
return SysResult.build(200,"",null);
}
}
MyBatis Mapper文件配置
<?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="liwen.zhao.mappers.UserMapper">
<resultMap id="cus" type="liwen.zhao.common.jd.pojo.CommonUser">
<id column="id" property="id"/>
<result column="userName" property="userName"/>
<collection property="roles" ofType="liwen.zhao.common.jd.pojo.Role">
<id column="roleid" property="id"/>
<result column="ppid" property="parent_id"/>
<result column="role_name" property="role_name"/>
<result column="gen_time" property="gen_time"/>
<result column="description" property="description"/>
<collection property="rightList" ofType="liwen.zhao.common.jd.pojo.Right">
<id column="riid" property="id"/>
<result column="right_name" property="right_name"/>
<result column="right_type" property="right_type"/>
</collection>
</collection>
</resultMap>
<select id="findCommonUserByNameAndPswAndAction" resultMap="cus">
SELECT *,tsb_common_user_role_relation.id AS currid,
tsb_role_right_relation.id AS rrrid,
tsb_right.id AS riid,
tsb_role.id as roleid,
tsb_role.parent_id as ppid FROM tsb_common_user
RIGHT JOIN tsb_common_user_role_relation ON tsb_common_user.id=tsb_common_user_role_relation.uid
LEFT JOIN tsb_role ON tsb_role.id=tsb_common_user_role_relation.rid
LEFT JOIN tsb_role_right_relation ON tsb_role_right_relation.role_id=tsb_common_user_role_relation.rid
LEFT JOIN tsb_right ON tsb_right.id=tsb_role_right_relation.right_id
<where>
<if test="userName !=null">
tsb_common_user.userName=#{userName}
</if>
<if test="action !=null">
and right_name=#{action}
</if>
<if test="psw !=null">
and psw=#{psw}
</if>
</where>
</select>
</mapper>
前端JS实现
<script>
function checkForm(){
$("#sub").html("<i class=\"fas fa-1x fa-sync-alt fa-spin\"></i> Sign In");
var userName=$("input[name='userName']").val();
var psw=$("input[name='psw']").val();
$.post("http://localhost/user/adminLogin",{"username":userName,"psw":psw,},function (result) {
$("#sub").html("Sign In");
if(result.status!=200){
alert(result.msg);
}else{
$(window).attr('location','http://localhost/admin');
}
})
return false;
}
</script>
最终效果
密码错误提示:
无权访问提示:
用户名密码正确,并且有权访问,调转主页(注:暂未实现登录状态存储):
代码地址:https://gitee.com/zhaoliwen/JD_ALL.git