- 用户登录
动态生成秘钥:
@Service //将类交给Spring容器
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public String findUP(User u) {
byte[] bytes = u.getPassword().getBytes();
String md5pwd = DigestUtils.md5DigestAsHex(bytes);
u.setPassword(md5pwd);
User byUP = userMapper.findByUP(u);
if (byUP==null){
return null;
}
return UUID.randomUUID().toString().replace("-", "");
}
}
Cookie和Session:
业务说明:用户向服务器发起请求,获取结果时请求周期是一次请求一次响应,请求结束则响应的数据清空,为了保存服务器的响应数据,应该想办法将服务器数据持久化
Session相关:
1.称为“会话控制”
2.可以存储用户信息
3.生命周期是整个会话,会话关闭则数据清空
4.数据结构为key-value
5.浏览器session存储的位置(F12—Application)
Cookie相关:
1.是一个小型文本文件,存储在本地终端上
2.可以存储用户信息
3.数据结构是key-value
4.数据一般采用加密的方式保存
5.数据可以永久保存
Cookie和Session选择(特点):
1.数据需要临时保存,则选择Session,长时间存储则选择Cookie
2.对安全性要求高,则选择Session,要求不高则选择Cookie
用户登录实现数据存储:
用户登录成功后,将秘钥保存到Session中,后期需要校验Session,如果token中有值,则代表用户登录过,没有值则说明用户没有登录过,跳转到登录页面
- 登录后页面跳转
编辑路由(router/index.js):
import Home from '../components/Home.vue'
{path: '/home',component: Home, children:[
{path: '/user', component: User},
{path: '/itemCat', component: ItemCat},
{path: '/item', component: Item},
{path: '/item/addItem', component: AddItem}
],
}
- Vue的路由导航守卫
访问BUG:
用户可以直接根据网址访问其他页面,如果想控制该漏洞,则需要在前端进行权限的校验
规则:如果用户访问没有token信息,则表示用户没有登录,则需要跳转到登录页面
解决方式:拦截器(老东西),新的可使用路由导航守卫,本质也是一个拦截器
定义路由导航守卫:
参数1. to 路由跳转的网址
参数2. from 路由从哪里来
参数3. next 是一个函数,表示放行或重定向,next() 放行,next("/login") 重定向
核心逻辑: 检查是否有token. 如果访问login页面 直接放行.没有token 表示用户没有登录,重定向到登录页面
router.beforeEach((to,from,next) => {
if(to.path === "/login"){
return next()
}
//说明用户访问的页面不是login 请求需要校验
//获取token数据.
let token = window.sessionStorage.getItem("token")
//if(token !==null && token.length>0)
//下列if 解释为: 如果token不为null
if(token){
return next()
}
next("/login")
})
- 左侧菜单业务实现
权限层级代码结构:
SQL语句:
SELECT p.id,p.name,p.parent_id,p.level,p.created,p.updated,
c.id c_id,c.name c_name,c.parent_id c_parent_id,c.path c_path,c.level c_level,c.created c_created,c.updated c_updated FROM
(SELECT * FROM rights WHERE parent_id=0) p
LEFT JOIN rights c
ON p.id=c.parent_id
重难点(配置文件):
<select id="getRightsList" resultMap="rightRM">
SELECT p.id,p.name,p.parent_id,p.level,p.created,p.updated,
c.id c_id,c.name c_name,c.parent_id c_parent_id,c.path c_path,c.level c_level,c.created c_created,c.updated c_updated FROM
(SELECT * FROM rights WHERE parent_id=0) p
LEFT JOIN rights c
ON p.id=c.parent_id
</select>
<resultMap id="rightRM" type="Rights" autoMapping="true">
<id property="id" column="id"></id>
<!-- 封装一对多数据-->
<collection property="children" ofType="Rights">
<id property="id" column="c_id"></id>
<result property="name" column="c_name"></result>
<result property="parentId" column="c_parent_id"></result>
<result property="path" column="c_path"></result>
<result property="level" column="c_level"></result>
<result property="created" column="c_created"></result>
<result property="updated" column="c_updated"></result>
</collection>
</resultMap>