SpringBoot基础学习之(二十):Shiro与Thymeleaf的整合版本

还是一样,本篇文章是在上一篇文章的基础上,实施再次进阶

Shiro是一种特别的流行的安全框架,Thymeleaf则是spring boot架构中使用的一种特别引擎。今天介绍的则是它们俩的整合版本。

实现的功能:前端的显示的内容,是根据登录用户的权限来定,不同权限的用户则看到的内容也是不同。

一:导入整合版本的依赖

        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>

二:在自定义的配置类中定义整合 Thymeleaf和shiro

    //通过ShiroDialect框架:整合thymeleaf和shiro
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

三:设置session属性,传递给前端,用以判断用户是否登录

在认证重写的方法中获取session

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("loginuser",user);

 完整代码

package com.springboot_shiro.Myconfig;


import com.springboot_shiro.pojo.user;
import com.springboot_shiro.service.userService;
import org.apache.shiro.SecurityUtils;
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.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class Realm extends AuthorizingRealm {
    @Autowired
    com.springboot_shiro.service.userService userService;

    //授权内容则是在这个接口进行配置
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权");
        //设置来到此页面的用户给他添加一个权限 add
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //获取当前用户的信息:数据源为下面认证时SimpleAuthenticationInfo类传递过来的
        Subject subject = SecurityUtils.getSubject();
        user principal = (user)subject.getPrincipal();
        //将获取权限值,赋给此时登录的用户
        authorizationInfo.addStringPermission(principal.getPerm());
        return authorizationInfo;
    }


    //验证则是在这个接口进行配置
    //获取subject传递来的参数加密的令牌Token,进行认证
    //AuthenticationInfo是一个接口:return它的的实现类

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken Token) throws AuthenticationException {

        UsernamePasswordToken token =(UsernamePasswordToken) Token;  //转换
        user user = userService.queryUser(token.getUsername());

        //向前端传递参数session值
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("loginuser",user);
        System.out.println(token.getUsername());
        if (user==null){
            return null;
        }
        //密码的验证,在spring boot架构中给一个类SimpleAuthenticationInfo可以自动化进行认证
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}

四:在前端进行权限配置

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<hr/>
<!--判断用户是否已经登录-->
<div th:if="${session.loginuser==null}">
<a th:href="@{/tologin}">登录</a>
</div>
<!--只有用户的权限具有add权限,才能成功显示-->
<div shiro:hasPermission="user:add">
<a th:href="@{/add}">add</a>
</div>
<!--只有用户的权限具有update权限,才能成功显示-->
<div shiro:hasPermission="user:update">|
<a th:href="@{/update}">update</a>
</div>
</body>
</html>

运行项目:

没有用户进行登录时:

 数据库信息:

 登录demo1用户时

 登录demo2用户

 登录用户demo3时

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不想睡醒的梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值