SpringBoot整合Shiro

SpringBoot整合Shiro

在这里插入图片描述

  • Apache Shiro是一个功能强大且易于使用的Java安全框架,用于执行身份验证,授权,加密和会话管理。
  • 使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序-从最小的移动应用程序到最大的Web和企业应用程序

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

有哪些功能

在这里插入图片描述
在这里插入图片描述

一、环境搭建

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

1.导入shiro-spring依赖

导入shiro整合springboot的包

<!--shiro整合springboot-->
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.8.0</version>
</dependency>

2.编写shiro配置类

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

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

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

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

//自定义的UserRealm需要extend AuthorizingRealm
public class UserRealm extends AuthorizingRealm {   
   //授权
   @Override
   protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
      System.out.println("执行了=>授权doGetAuthorizationInfo");      
      return null;
   }
   
   //认证
   @Override
   protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
      System.out.println("执行了=>认证doGetAuthenticationInfo");
       return null;
   }
}

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

  • ShiroFilterFactoryBeanshiro过滤工厂对象:该对象需要关联SecurityManager对象,同样在参数中传入该对象的参数,用@Qualifier指定需要的实现类方法名
  • DefaultWebSecurityManager默认安全管理器:该对象需要关联realm对象,在方法参数中传入realm对象的参数,用@Qualifier指定需要的realm实现类UserRealm方法名即可
  • realm安全实体数据源:用我们自定义的UserRealm类来创建
@Configuration
public class ShiroConfig {   
   //ShiroFilterFactoryBean
   @Bean
   public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
      ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
      //设置安全管理器
      bean.setSecurityManager(defaultWebSecurityManager);       
      return bean;
   }
   
   //DefaultWebSecurityManager
   @Bean(name = "securityManager")
   public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
      DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();     
      //关联UserRealm
      securityManager.setRealm(userRealm);      
      return securityManager;
   }
   
   //创建realm对象,自定义
   @Bean
   public UserRealm userRealm(){
      return new UserRealm();
   } 
}

二、Shiro实现登录拦截

1.编写页面及其controller

在templates目录下新建user包,编写add.htmlupdate.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

在这里插入图片描述

点击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("/toLogin");
    return bean;
}

在这里插入图片描述

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

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

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

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thyemleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登录</h1>
<hr>
<p th:text="${msg}" style="color: red" ></p>
<form th: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("/toLogin")
public String toLogin() {
   return "login";
}

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

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

三、Shiro实现用户认证

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

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

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

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

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

  • username:用户名
  • password:密码

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

MyController类中增添login方法

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

在这里插入图片描述

发现执行了自定义UserRealm中的doGetAuthenticationInfo认证方法

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

//认证
@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, "azmat");
}

四、整合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.8</version>
</dependency>
<!--log4j-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<!--springboot-mybatis-->
 <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

2. 配置数据源

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

spring:
  datasource:
    username : root
    password: 1127
    url: jdbc:mysql://localhost:3307/mybatis?&useUnicode=true&characterEncoding=UTF-8
    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

3. 编写pojo实体类

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

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

4. 编写Mapper层

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

@Repository
@Mapper
public interface UserMapper {
   User queryUserByName(String name);
}

然后编写对应的mapper.xml,在resources目录下新建com/example/demo/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.example.demo.mapper.UserMapper">
    <select id="queryUserByName" parameterType="String" resultType="User">
        select * from user where name=#{name};
    </select>
</mapper>

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

#设置别名
mybatis.type-aliases-package=com.example.demo.pojo
#绑定mapper.xml
mybatis.mapper-locations=classpath:mybatis/*.xml

6. 编写service层(可省略)

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

package com.example.demo.service;

import com.example.demo.pojo.User;

public interface UserService {
   User queryUserByName(String name);
}
@Service
public class UserServiceImpl implements UserService{
   
   @Resource
   UserMapper userMapper;
   
   @Override
   public User queryUserByName(String name) {
      return userMapper.queryUserByName(name);
   }
}

7. 测试

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

@SpringBootTest
class Springboot5ShiroApplicationTests {
   
   @Resource
   UserServiceImpl userService;
   
   @Test
   void contextLoads() {
      System.out.println(userService.queryUserByName("azmat"));
   }
}

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

在这里插入图片描述

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

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

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

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(), "");
    }
}

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

五、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("/toLlogin");
    return bean;
}

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;
}

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

在这里插入图片描述

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

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(), "");
    }
}

如果登录azmat用户,只有add权限

如果登录aize用户,有所有的权限

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

六、Shiro整合thymeleaf

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

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

1. 导入依赖

<!--shiro-thymeleaf整合 -->
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.1.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</title>
</head>
<body>
	<h1>首页</h1>
    <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>

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

//认证
   @Override
   protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
      System.out.println("执行了=>认证doGetAuthenticationInfo");
      
//    //用户名,密码  数据中获取
//    String name="root";
//    String password="123456";
      
      UsernamePasswordToken userToken= (UsernamePasswordToken) token;
      
//    if (!userToken.getUsername().equals(name)){
//       return null;//抛出异常 UnknownAccountException
//    }
      
      //连接真实的数据库
      User user = userService.queryUserByName(userToken.getUsername());
      if (user==null){//用户名认证
         return null; //只需要return null,就会自动抛出UnknownAccountException一场
      }
      
      Subject currentSubject = SecurityUtils.getSubject();
      Session session        = currentSubject.getSession();
      session.setAttribute("loginUser",user);
      
      //密码认证(md5加密,md5盐值加密),涉及到安全问题,shiro自动完成
      return new SimpleAuthenticationInfo(user,user.getPwd(),"");
   }
<!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</title>
</head>
<body>
<!--如果我们还想实现如果登录成功就不显示登录链接了呢?-->
<h1>首页</h1>
<div th:if="${session.loginUser}==null">
    <a th:href="@{/toLogin}">登录</a>
</div>

<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>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值