spring boot 前后端分离整合shiro(四)配置加密器以及Subject的使用

spring boot 前后端分离整合shiro(四)配置加密器以及Subject的使用

ShiroConfig添加加密器bean

/**
     * 加密器
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置散列算法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列次数 表示加密几次 此处为加密十次
        hashedCredentialsMatcher.setHashIterations(10);
        return hashedCredentialsMatcher;
    }

然后添加到realm中去
在这里插入图片描述
写个登录接口测试一下。

package com.example.demo.controller;

import com.example.demo.entity.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 1. @author 黄豪琦
 2. 日期:2019-07-09 16:04
 3. 说明:
 */
@RestController
@RequestMapping("/pub")
public class LoginController {

    @PostMapping("/login")
    public String login(UserInfo user){
        String result;
        //1
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(), user.getPassword());
        //2
        Subject subject = SecurityUtils.getSubject();
        try {
            //3
            subject.login(token);
            result = "成功";
        } catch (UnknownAccountException e){
            result = e.getMessage();
        }catch (IncorrectCredentialsException e){
            result = "密码错误";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            result = "认证失败";

        }

        return result;
    }
}

启动项目访问
在这里插入图片描述
返回一个密码错误就说明成功了。然后解释一下代码:

1.token :

将用户输入的用户名、密码、[是否选择记住我]进行一个封装,传递给shiro,数据的载体。

2.SecurityUtils.getSubject()

之前我们说过shiro中有三个概念,subject、securityManager和realm,通过subject去操作shiro,这行代码的作用就是获取当前的Subject。注意不要倒错包。

import org.apache.shiro.subject.Subject;

3. subject.login(token);

执行登录操作。shiro会去认证,一步步走到CustomizeRealm的doGetAuthenticationInfo方法里。如果认证出现问题就会抛出对应的异常,常用的有这几个:

  1. UnknownAccountException:没有找到这个用户时抛出
  2. IncorrectCredentialsException : 密码错误时抛出
  3. LockedAccountException: 用户被锁定时抛出
    所以我们只要在login的时候捕获对应的异常就能知道认证的结果。

为什么会密码错误

数据库里密码是111111,输入的密码也是111111,为什么会密码错误?
因为配置了密码比较器,在认证的时候,从数据库中拿出的密码是111111,而我们传入参数中的密码已经是被加密后的,所以肯定对不上。
写个测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void md5Test(){
        String hashName = "md5";
        String password = "111111";
        Object result = new SimpleHash(hashName, password, null, 10);
        System.out.println(result.toString());
    }
}

(注意SimpleHash(hashName, password, null, 10);中的参数要和ShiroConfig中的一样,密码要和数据库里的一样)
运行结果:
在这里插入图片描述
然后拷贝这一串手动去数据库修改密码
在这里插入图片描述
再次登录
在这里插入图片描述
显示成功。

Subject中的方法

常用的有
login(token)
执行登录操作。
getPrincipal()
得到当前登录用户的信息。就是在认证方法中存的东西,存的是user对象那么返回的就是user对象,存的登录名那返回的就是登录名。
在这里插入图片描述
isAuthenticated()
判断当前subjec是否以登录,返回true表示以登录。
hasRole(String s)
判断当前subject是否有某个角色,返回布尔值。参数对应授权方法中的放进去的值:
在这里插入图片描述
isPermitted(String s)
判断当前subject是否有某种权限,返回值、参数是授权方法中存入的值。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值