(一)工程建立到登录与认证-Spring boot、Shiro、Mybatis-Plus、MySql
(二)博客显示与登录后按钮按照Shiro授权显示加载html
首页显示与shiro授权
三、界面完善
3.1 静态文件
3.1.1 引入index页面
- 从项目源码中的index.html导入至
src/main/resources/templates/index.html
- 注释掉以下三个位置的div
# 注释掉这些
<div th:replace="lunbo :: lunbo"></div>
<div th:replace="left :: left"></div>
<div th:replace="aside :: aside"></div>
3.1.2 引入lasturl.html页面
从项目源码中的lasturl.html导入至src/main/resources/templates/lasturl.html
3.1.3 导入lastUrl.js
导入src/main/resources/static/style/js/lastUrl.js
3.2 创建HomeController类
- 将之前创建的TestController类删除
- 创建com.liang.modules.sys.controller.HomeController
内容如下/** * @description 跳转至首页 */ @Controller public class HomeController { @RequestMapping({"/", "/index"}) public String index(){ return "index"; } } /** * @description 跳转至登陆页面,并保存跳转前的页面位置lastUrl */ @GetMapping("login") public String login(HttpServletRequest request){ String lastUrl = request.getHeader("Referer");// 获取从哪里点进来的 if (StringUtils.isEmpty(lastUrl)){ return "login"; } if (lastUrl.contains("findPwd") || lastUrl.contains("register") || lastUrl.contains("login")){ //若为注册、找回密码等界面进的登陆连接则删除lastUrl属性,这样就可以在登陆完成时不会又重新跳转回注册、找回密码等链接 request.getSession().removeAttribute("lastUrl"); } else { //保存跳转页面之前的url request.getSession().setAttribute("lastUrl", lastUrl); } return "login"; } /** * @description 跳转到index.html或lasturl.html,保存存储的链接到response头 */ @GetMapping("lasturl") public String lastUrl(HttpServletRequest request, HttpServletResponse response){ System.out.println("进入了lastUrl"); String lastUrl = (String) request.getSession().getAttribute("lastUrl"); if (StringUtils.isEmpty(lastUrl)){ System.out.println("index ==> 1"); System.out.println(lastUrl); return "index"; } else { response.setHeader("lastUrl", lastUrl); System.out.println("index ==> 2"); System.out.println(lastUrl); return "lasturl"; } }
3.3 测试与分析
3.3.1 测试
运行未正常的,可查看target中
是否有新添加的静态文件,如没有可以先将target
删除重新编译生成
输入http://localhost:8080/显示如下,点击登陆,输入手机和密码,登陆成功后将正常返回至首页
3.3.2 分析
登录后未显示登录用户名,仍然显示登陆和注册两个按钮,这不是我们所需要的,这是由于我们还没做授权,下一步将做授权
四、授权
4.1 授权设置
4.1.1 重写授权方法
对com.liang.modules.sys.shiro.UserRealm
类中的doGetAuthorizationInfo
授权方法重写,之前我们重写的是认证方法
/**
* @method 授权方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//获取用户(前期认证已经加入进去了)
UserVOEntity user = (UserVOEntity) SecurityUtils.getSubject().getPrincipal();
Set<RoleVOEntity> roles = user.getRoles();
List<String> roleNameList = new ArrayList<>();
List<String> permissionList = new ArrayList<>();
for (RoleVOEntity role :
roles) {
roleNameList.add(role.getRoleName()); // 添加角色名称
for (PermissionEntity permission:
role.getPermissionSet()) {
permissionList.add(permission.getPermissionName()); // 添加权限名称
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); // 授权
info.addStringPermissions(permissionList);
info.addRoles(roleNameList);
return info;
}
4.1.2 shiro授权按钮显示配置
在com.liang.modules.sys.shiro.ShiroConfig
中添加一个方法
// Shiro控制ThymeLeaf界面按钮根据授权信息是否显示
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
4.2 测试及分析
4.2.1 测试
同理,运行,输入网址、登录,登录完成后上方的登录和注册按钮消失并显示为登录用户的名称,如图所示
4.2.1 分析
这里能做到根据授权信息决定是否显示登录、注册按钮的原因是huml中shiro web的支持,shiro标签根据当前的授权信息,调整前端页面标签的显示
例如可以查看header.html文件中,注册按钮的一段html程序,当未认证的时候即shiro:notAuthenticated=""
,标签内的将会显示
<div class="am-topbar-right" shiro:notAuthenticated="">
<a href="/register" class="am-btn am-btn-primary am-topbar-btn am-btn-sm"
style="color: white">注册</a>
</div>