还是一样,本篇文章是在上一篇文章的基础上,实施再次进阶
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时