Vue Cli3+SpringBoot踩坑历程(5)

本文记录了使用VueCli3和SpringBoot实现后端注册加密、登录解密及登出功能的过程。通过Shiro框架进行权限管理,详细描述了如何配置接口拦截和实现rememberMe功能,包括代码示例和项目结构图。
摘要由CSDN通过智能技术生成
今天跟着这位博主学习了后端注册加密和登陆解密功能。

1.说实话,我没搞懂Shiro框架在springboot中的运用。所以我上代码给自己记录一下自己干了什么:
在这里插入图片描述
首先是realm包的Realm类:

package realm;

import com.example.demo.User.User;
import com.example.demo.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

public class Realm extends AuthorizingRealm {
   
    @Autowired
    private UserService userService;
    // 简单重写获取授权信息方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection){
   
        SimpleAuthorizationInfo s = new SimpleAuthorizationInfo();
        return s;
    }

    // 获取认证信息,即根据 token 中的用户名从数据库中获取密码、盐等并返回
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException{
   
        String userName = token.getPrincipal().toString();
        User user = userService.getByName(userName);
        if(ObjectUtils.isEmpty(user)){
   
            throw new UnknownAccountException();
        }
        String passwordInDB = user.getPassword();
        String salt = user.getSalt();
        SimpleAuthenticationInfo authenticationInfo  = new SimpleAuthenticationInfo(userName,passwordInDB, ByteSource.Util.bytes(salt),getName());
        return authenticationInfo ;
    }
}

接着是User类添加了新的属性:
在这里插入图片描述
然后是result包的三个类:

package com.example.demo.result;

public class Result {
   
    private int code;
    private String message;
    private Object data;

    Result(int code, String message, Object data) {
   
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
   
        return code;
    }

    public void setCode(int code) {
   
        this.code = code;
    }

    public String getMessage() {
   
        return message;
    }

    public void setMessage(String message) {
   
        this.message = message;
    }

    public Object get
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值