SpringBoot整合Shiro安全框架完整实现



image-20201104155806233

  • Apache Shiro是一个功能强大且易于使用的Java安全框架,用于执行身份验证,授权,加密和会话管理。

  • 使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序-从最小的移动应用程序到最大的Web和企业应用程序。

官网https://shiro.apache.org/

Githubhttps://github.com/apache/shiro

PS:笔者是看着狂神老师的shiro视频来学习的,也推荐给大家:https://www.bilibili.com/video/BV1PE411i7CV?p=38

一、环境搭建

首先创建一个springboot项目,勾选组件时勾选Spring WebThymeleaf

1. 导入shiro-spring依赖

导入shiro整合springboot的包

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.6.0</version>
</dependency>

2. 编写首页及其controller

resources/templates目录下新建index.html首页,注意导入thymeleaf的命名空间,我们用th:text标签接收前端的参数msg并显示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<p th:text="${msg}"></p>
</body>
</html>

然后在主程序同级目录下新建controller包,其中新建MyController类,编写首页跳转的controller

其中给前端视图存值msg

package com.zsr.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
    @RequestMapping({"/index", "/"})
    public String toIndex(Model model) {
        model.addAttribute("msg", "Hello Shiro");
        return "index";
    }
}

3. 编写shiro配置类

在主程序同级目录下新建config包,其中新建ShiroConfig配置类

其中需要配置三大对象并将其注入到spring容器中:

  • realm对象:可看作安全实体的数据源,该对象需要自定义,继承AuthorizingRealm
  • DefaultWebSecurityManager对象:默认安全管理器实体
  • ShiroFilterFactoryBean对象:Shiro过滤工厂实体

首先编写自定义的realm类UserRealm,只需要继承AuthorizingRealm类,重写其认证和授权的方法

package com.zsr.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了=>授权doGetAuthorizationInfo");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了=>认证doGetAuthorizationInfo");
        return null;
    }
}

然后编写shiro的配置类ShiroConfig,其中声明三个对象:

  • realm安全实体数据源:用我们自定义的UserRealm类来创建
  • DefaultWebSecurityManager默认安全管理器:该对象需要关联realm对象,在方法参数中传入realm对象的参数,用@Qualifier指定需要的realm实现类UserRealm方法名即可
  • ShiroFilterFactoryBeanshiro过滤工厂对象:该对象需要关联SecurityManager对象,同样在参数中传入该对象的参数,用@Qualifier指定需要的实现类方法名
package com.zsr.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    //创建realm对象,自定义
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    //DefaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        return bean;
    }
}


二、Shiro实现登录拦截

1. 编写页面及其controller

在templates目录下新建user包,编写add.htmlupdate.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add</title>
</head>
<body>
<h1>add</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update</title>
</head>
<body>
<h1>update</h1>
</body>
</html>

然后在MyController中编写对应的controller

@RequestMapping("/user/add")
public String add() {
    return "user/add";
}

@RequestMapping("/user/update")
public String update() {
    return "user/update";
}

然后在首页上增加相应跳转的链接

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<p th:text="${msg}"></p>
<hr>
<a th:href="@{/user/add}">add</a> | <a th:href="@{/user/update}">update</a>
</body>
</html>

到此,我们的基本环境搭建完成

启动主程序测试一下,访问localhost:8080
image-20201018133002954
点击add即可跳转到add.html,点击update即可跳转到update.html

2. 实现登录拦截

在shiro配置类中,我们创建了三个对象,要实现登录拦截功能,就要用到shiro过滤工厂对象

我们在配置类ShiroConfigshiroFilterFactoryBean()方法中添加shiro的拦截器,实现登录过滤的功能

shiroFilterFactoryBean()方法中设置拦截器setFilterChainDefinitionMap

//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
    ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
    //设置安全管理器
    bean.setSecurityManager(defaultWebSecurityManager);
    //添加shiro内置的过滤器
    /*
            anon:无需认证就可以访问
            authc:必须认证了才能访问
            user:必须拥有记住我功能才能使用
            perms:拥有对某个资源的权限才能访问
            role:拥有某个角色权限才能访问
         */
    Map<String, String> filterMap = new LinkedHashMap<>();//链式
    filterMap.put("/user/add", "anon");
    filterMap.put("/user/update", "authc");
    //filterMap.put("/user/*", "authc");支持通配符
    bean.setFilterChainDefinitionMap(filterMap);//参数为map类型
    //设置登录的请求
    bean.setLoginUrl("/login");
    return bean;
}

再次重启主程序访问测试一下,同样访问localhost:8080

点击add可以正常访问,点击update无法正常访问,这是因为拦截器的作用,/user/add请求无需认证就可以访问,但是/user/authc请求需要认证才能访问
image-20201018133247454
image-20201018133256387

3. 编写拦截后的登录页面

上述代码成功实现拦截的功能,但是我们被拦截后应该跳转到登陆页面,因此我们需要创建一个登录页面,在templates目录下新建一个login.html登录页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
<h1>登录</h1>
<form action="/login">
    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="text" name="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

然后编写视图跳转的contoller

@RequestMapping("/login")
public String login() {
    return "login";
}

然后在上述配置类方法中中设置登录的请求

bean.setLoginUrl("/login");//设置登录的请求

启动主程序测试一下,访问8080端口,点击add成功显示add.html,但是点击update则会跳转到登陆页面,成功~
image-20201018134535009



三、Shiro实现用户认证

上述创建Realm对象中,我们继承了AuthorizingRealm类,重写了其两个方法,shiro实现用户认证的功能,也就是在其中的认证方法doGetAuthenticationInfo中完成的,我们来测试测试

同官方案例的Quickstart源码一样,可以看我的上一篇博客:Shiro第一个程序:官方快速入门程序Qucickstart详解教程

实现用户认证有几个步骤:

  1. 获取当前用户
  2. 封装用户信息生成token令牌
  3. 执行登录操作(可以自定义捕获异常)

我们将这些代码编写在一个controller中,其中需要传入两个参数

  • username:用户名
  • password:密码

这两个参数是通过前端登录页面传送的

MyController类中增添toLogin方法

@RequestMapping("/toLogin")
public String toLogin(String username, String password, Model model) {
    //获取当前用户
    Subject subject = SecurityUtils.getSubject();
    //封装用户信息生成token令牌
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    //执行登录操作,可以自定义捕获异常
    try {
        subject.login(token);
        return "index";//登录成功返回首页
    } catch (UnknownAccountException e) {
        model.addAttribute("msg", "用户名不存在");
        return "login";//用户名错误回到登录页面
    } catch (IncorrectCredentialsException e) {
        model.addAttribute("msg", "密码不正确");
        return "login";//证书
    }
}

然后登录页面点击提交跳转到/toLogin请求,即进入到上述方法,并且接收传入的msg

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
<h1>登录</h1>
<p th:text="${msg}" style="color: red"></p>
<form action="/toLogin">
    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="text" name="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

我们启动测试一下:访问localhost:8080/login
image-20201028142143575
随便输入用户名密码,然后点击登录,可以看到显示用户名不存在

同时查看控制台信息
image-20201028142258604
发现执行了自定义UserRealm中的doGetAuthenticationInfo认证方法

//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    System.out.println("执行了=>认证doGetAuthorizationInfo");
    return null;
}j

也就是只要我们点击登录,就会执行认证方法,因此我们需要在该方法中添加认证用户信息代码

//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    System.out.println("执行了=>认证doGetAuthorizationInfo");
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    //伪造正确用户名和密码
    String username = "zsr";
    String password = "200024";
    //用户名认证
    if (!token.getUsername().equals(username))
        return null;//只需要return null,就会自动抛出UnknownAccountException异常
    //密码认证,涉及到安全问题,shiro自动完成
    return new SimpleAuthenticationInfo("", password, "zsr");
}

再次重启测试一下,输入错误的用户名和密码
image-20201028143032285
显示用户不存在
image-20201028143141437
如果输入正确的用户名和错误的密码
image-20201028143157018
则会显示密码不正确
image-20201028143249401
如果输入正确的用户名和密码
image-20201028143308348
则成功登录到首页
image-20201028143318954



四、整合MyBatis

上述实现了简单的用户认证,实际开发中,所有的用户信息都在数据库中,因此现在来整合数据库进行使用

1. 导入依赖

这里我们使用druid数据源,导入三个依赖:

  • mysql连接驱动
  • druid数据源
  • log4j(配合druid数据源)
  • lombok(方便后续实体类)
<!--MySQL连接驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Druid数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.1</version>
</dependency>
<!--log4j-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
</dependency>
<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>
<!--springboot-mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

2. 创建数据库&连接

首先创建一个数据库shiro_mybatis

-- 创建数据库
CREATE DATABASE shiro_mybatis;

-- 使用shiro_myabtis数据库
use shiro_mybatis;

-- 创建user表
CREATE TABLE IF NOT EXISTS `user`(
	`id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '身份号',
	`name` VARCHAR(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
	`pwd` VARCHAR(30) NOT NULL DEFAULT '123456' COMMENT '密码',
	PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

-- 给user表插入数据
INSERT INTO `user`(`id`,`name`,`pwd`) 
VALUES ('1','zsr',000204),('2','gcc',000421),('3','BaretH',200024);

然后idea连接该数据库并打开
image-20201028144744600
image-20201028232151515
数据表
image-20201028144834309

3. 配置数据源

新建spring-application.yaml,配置数据库连接信息和druid数据源的专有配置

spring:
  datasource:
    username: root
    password: 200024
    url: jdbc:mysql://localhost:3306/shiro_mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters
    # stat:监控统计
    # log4j:日志记录(需要导入log4j依赖)
    # wall:防御sql注入
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

4. 编写pojo实体类

在主程序同级目录下新建pojo包,其中新建User

package com.zsr.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

5. 编写Mapper层

在主程序同级目录下新建mapper包,其中新建UserMapper接口

package com.zsr.mapper;

import com.zsr.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper //表示这是Mybatis的mapper类
@Repository
public interface UserMapper {
    //通过用户名查询用户
    public User queryUserByName();
}

然后编写对应的mapper.xml,在resources目录下新建mapper包,在其中新建UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsr.mapper.UserMapper">
    <select id="queryUserByName" parameterType="String" resultType="com.zsr.pojo.User">
        select * from shiro_mybatis.user where name=#{name};
    </select>
</mapper>

然后需然后要在springboot核心配置文件中绑定该UserMapper.xml文件

#绑定mapper.xml
mybatis:
  mapper-locations: classpath:mapper/*.xml

6. 编写service层(可省略)

在主程序同级目录下新建service包,其中新建UserServiceUserServiceImpl两个类

package com.zsr.service;

import com.zsr.pojo.User;

public interface UserService {
    public User queryUserByName(String username);
}
package com.zsr.service;

import com.zsr.mapper.UserMapper;
import com.zsr.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryUserByName(String username) {
        return userMapper.queryUserByName(username);
    }
}

7. 测试

在springboot提供的测试类中进行测试,根据用户名查询用户

package com.zsr;

import com.zsr.service.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot07ShiroApplicationTests {
    @Autowired
    private UserServiceImpl userService;

    @Test
    void contextLoads() {
        System.out.println(userService.queryUserByName("zsr"));
    }
}

运行测试一下,成功查到指定用户
image-20201028232936434

8. 更改伪造数据为真实数据

到此,整合mybatis完毕,我们可以将上述伪造的用户数据用数据库来替代,我们修改UserRealm中认证方法的相关代码

首先要注入UserServiceImpl对象,然后将伪造的数据更改为数据库中真实的数据

package com.zsr.config;

import com.zsr.pojo.User;
import com.zsr.service.UserServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserServiceImpl userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了=>授权doGetAuthorizationInfo");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了=>认证doGetAuthorizationInfo");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //连接真实的数据库
        User user = userService.queryUserByName(token.getUsername());
        //用户名认证
        if (user == null)
            return null;//只需要return null,就会自动抛出UnknownAccountException一场
        //密码认证,涉及到安全问题,shiro自动完成
        return new SimpleAuthenticationInfo("", user.getPwd(), "zsr");
    }
}

然后再次重启主程序进行测试,进入到登录页面,只要输入数据库中正确的用户名和密码即可实现登录
image-20201028234101357
image-20201028234135337
image-20201028234151698



五、Shiro请求授权实现

1. 添加授权

要实现登录拦截功能,同样通过shiro过滤工厂设置权限:

在shiro配置类ShiroConfig中的shiroFilterFactoryBean()方法中添加相关代码实现请求授权

//设置授权,只有user:add权限的才能请求/user/add
filterMap.put("/user/add", "perms[user:add]");
//设置授权,只有user:update权限的才能请求/user/update
filterMap.put("/user/update", "perms[user:update]");
//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
    ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
    //设置安全管理器
    bean.setSecurityManager(defaultWebSecurityManager);
    //添加shiro内置的过滤器
    /*
            anon:无需认证就可以访问
            authc:必须认证了才能访问
            user:必须拥有记住我功能才能使用
            perms:拥有对某个资源的权限才能访问
            role:拥有某个角色权限才能访问
         */
    Map<String, String> filterMap = new LinkedHashMap<>();//链式
    filterMap.put("/user/add", "perms[user:add]");//设置授权,只有user:add权限的才能请求/user/add
    filterMap.put("/user/update", "perms[user:update]");
    bean.setFilterChainDefinitionMap(filterMap);//参数为map类型
    //设置登录的请求
    bean.setLoginUrl("/login");
    return bean;
}

然后启动主程序测试一下,首先访问登录页面localhost:8080/login
image-20201028235952915
输入正确的用户名和密码点击提交登录进入到登录页面
image-20201029000058759
点击update或者add,会提示未授权
image-20201029000348501

2. 编写未授权页面

我们编写一个未授权页面,当没有权限时,跳转到该页面

MyController中添加未授权页面跳转的controller,即未授权跳转到该请求显示字符串

@RequestMapping("/unauthorized")
@ResponseBody
public String unauthorized() {
    return "未授权,无法访问此页面";
}

然后同样在shiroFilterFactoryBean方法中设置未授权页面的请求

//设置未授权页面的请求
bean.setUnauthorizedUrl("/unauthorized");

启动测试一下,同样按照刚才的测试,则跳转到未授权页面

3. 给用户授予权限

我们上述设置了权限,但是还没有给用户赋予对应的权限,我们接下来在UserRealm的授权方法中进行授权

这里是给了所有的用户都赋予user:add的权限

//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    System.out.println("执行了=>授权doGetAuthorizationInfo");
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addStringPermission("user:add");
    return info;
}

我们重启访问,成功登录后可以访问/add页面

但是对用户的授权不应该放在此,应该设置在数据库中,我们给user表新增一个字段perms,用于表示用户的权限信息
image-20201029125319919

image-20201029125332575

然后给不同的用户添加不同的权限

image-20201029125411565 然后同步实体类
package com.zsr.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
    private String perms;
}

然后我们需要在授权方法中拿到当前用户的资源,这时候只需要将认证方法中的principal参数传入,即可取出

package com.zsr.config;

import com.zsr.pojo.User;
import com.zsr.service.UserServiceImpl;
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.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserServiceImpl userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了=>授权doGetAuthorizationInfo");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取当前subject
        Subject subject = SecurityUtils.getSubject();
        //通过subject获取当前user
        User CurrentUser = (User) subject.getPrincipal();
        //设置当前user的权限(从数据库中读取)
        info.addStringPermission(CurrentUser.getPerms());
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了=>认证doGetAuthorizationInfo");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //连接真实的数据库
        User user = userService.queryUserByName(token.getUsername());
        //用户名认证
        if (user == null)
            return null;//只需要return null,就会自动抛出UnknownAccountException一场
        //密码认证(md5加密,md5盐值加密),涉及到安全问题,shiro自动完成
        return new SimpleAuthenticationInfo(user, user.getPwd(), "zsr");
    }
}

重启测试一下,我们登录zsr用户,可以成功进入add页面,无法进入update页面
image-20201029164847279
image-20201029164912713
如果登录gcc用户,则两个页面都没有权限

如果登录BaretH用户,可以成功进入update页面,无法进入add页面



六、Shiro整合thymeleaf

如果我们想实现在首页,拥有对应权限的用户只显示对应的超链接

这时候就可以通过Thymeleaf来完成,

1. 导入依赖

<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

2. 编写配置

在shiro配置类ShiroConfig中编写对应的配置

//配式shiro整合thymeleaf
@Bean
public ShiroDialect shiroDialect() {
    return new ShiroDialect();
}

3. 修改index.html

我们要实现在首页,拥有对应权限的用户只显示对应的超链接,然后添加一个登录按钮

首先导入shiro的命名空间

xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
	<h1>首页</h1>
    <p>
        <a th:href="@{/login}">登录</a>
    </p>
    <p th:text="${msg}"></p>
    <hr>
    <div shiro:hasPermission="user:add">
        <a th:href="@{/user/add}">add</a>
    </div>
    <div shiro:hasPermission="user:update">
        <a th:href="@{/user/update}">update</a>
    </div>
</body>
</html>

重启测试,访问http://localhost:8080/
image-20201029171204221
由于当前未登录,所以两个跳转链接都不显示;我们点击登录zsr用户
image-20201029171248798
可以看到只显示add链接
image-20201029171318884
同样,如果我们登录gcc用户,则两个都不显示
image-20201029171359293
如果我们登录BaretH用户,则只显示update链接
image-20201029171434976
如果我们还想实现如果登录成功就不显示登录链接了呢?

我们可以用session来完成,当用户登录后,将其session存入,然后前端判断session是否为空来显示

//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    System.out.println("执行了=>认证doGetAuthorizationInfo");
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    //连接真实的数据库
    User user = userService.queryUserByName(token.getUsername());
    //用户名认证
    if (user == null)
        return null;//只需要return null,就会自动抛出UnknownAccountException一场
    //将用户信息存入session
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    session.setAttribute("loginUser",user);
    //密码认证(md5加密,md5盐值加密),涉及到安全问题,shiro自动完成
    return new SimpleAuthenticationInfo(user, user.getPwd(), "zsr");
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<p th:if="${session.loginUser}==null">
    <a th:href="@{/login}">登录</a>
</p>
<p th:text="${msg}"></p>
<hr>

<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/user/update}">update</a>
</div>

</body>
</html>

再次测试,登陆成功后不显示登录连接了
image-20201029173704627

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Baret-H

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

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

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

打赏作者

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

抵扣说明:

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

余额充值