Shiro应用:笔记

37 篇文章 0 订阅
4 篇文章 1 订阅

1、获取当前登录人信息

2、登录验证的异常列表

3、登出操作

4、全局异常中捕获Shiro无权限异常

5、授权的注解

6、判断是否登录

 

1、获取当前登录人信息

//获取当前登录人
User currentUser = (User) SecurityUtils.getSubject().getPrincipal();

 

2、登录验证的异常列表

/**
 * 登录操作
 */
@RequestMapping("/login")
public String login(String loginAccount, String loginPassword)
{
    //参数验证
    if (UtilHelper.isNullAndEmpty(loginAccount) || UtilHelper.isNullAndEmpty(loginPassword))
    {
        return "login.html";
    }

    //账号密码令牌
    AuthenticationToken token = new UsernamePasswordToken(loginAccount, loginPassword);

    //获得当前用户到登录对象,现在状态为未认证
    Subject subject = SecurityUtils.getSubject();

    try
    {
        //将令牌传到shiro提供的login方法验证,需要自定义realm
        subject.login(token);

        //没有异常表示验证成功,进入首页
        return "redirect:/index";
    }
    catch (IncorrectCredentialsException ice)
    {
        request.setAttribute("message", "用户名或密码不正确!");
    }
    catch (UnknownAccountException uae)
    {
        request.setAttribute("message", "未知账户!");
    }
    catch (LockedAccountException lae)
    {
        request.setAttribute("message", "账户被锁定!");
    }
    catch (DisabledAccountException dae)
    {
        request.setAttribute("message", "账户被禁用!");
    }
    catch (ExcessiveAttemptsException eae)
    {
        request.setAttribute("message", "用户名或密码错误次数太多!");
    }
    catch (AuthenticationException ae)
    {
        request.setAttribute("message", "验证未通过!");
    }
    catch (Exception e)
    {
        request.setAttribute("message", "验证未通过!");
    }

    return "login.html";
}
异常类型异常说明
DisabledAccountException禁用的帐号
LockedAccountException锁定的帐号
UnknownAccountException错误的帐号
ExcessiveAttemptsException登录失败次数过多
IncorrectCredentialsException错误的凭证(用户名或密码错误)
ExpiredCredentialsException过期的凭证

 

3、登出操作

/**
 * 登出操作
 */
@RequestMapping("/logout")
public String logout()
{
    //登出清除缓存
    Subject subject = SecurityUtils.getSubject();
    subject.logout();

    return "redirect:/toLoginView";
}

 

4、全局异常中捕获Shiro无权限异常

/**
 * Shiro无权限异常
 */
@ExceptionHandler(UnauthorizedException.class)
public ModelAndView unauthorizedExceptionHandler(UnauthorizedException ex)
{
    //获取当前登录人
    StaffInfo currentSaff = (StaffInfo) SecurityUtils.getSubject().getPrincipal();

    //记录异常日志
    String logMeg = String.format("[403无权限] 操作人:%s;异常信息:%s;",currentSaff.getStaffName(),ex.toString());
    logger.error(logMeg);

    //返回403无权限页面
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("common/403.html");
    return modelAndView;
}

 

5、授权的注解

Shiro的授权可以使用@RequiresPermissions或者@RequiresRoles注解。

/**
 * 查询用户
 */
@RequiresPermissions("user:list")
@RequestMapping("/searchUser")
public String searchUser()
{
    request.setAttribute("message","查询用户");
    return "user.html";
}

/**
 * 多个授权
 */
@RequestMapping("/toStaffOperView")
@RequiresPermissions({"staff:add","staff:edit"})
public ModelAndView toStaffOperView(@RequestParam(value = "staff_id", defaultValue = "0") int staffId)
{
    
}

 

6、判断是否登录

Subject subject = SecurityUtils.getSubject();
boolean isLogin = subject.isAuthenticated();

isLogin为true表示已登录,为false表示未登录。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值