Spring Boot整合Shiro

Spring Boot+Shiro

Shiro这个Java安全框架我一直都想学会怎么去使用,但每次依照着别人的博客尝试把它配置到自己的项目中,总是出现各种问题,导致一直没有成功。经过不懈努力,这一次终于成功了!从零搭建整个项目,并通过一个简单的用户登录功能来进行说明!

源码github地址https://github.com/Rhine404/shirodemo

1 简介

推荐阅读官方文档去认识Shiro,虽然不是中文的,但绝对是比看各种博客里重复度高、零零散散的资料好。

官方的简介如下:

Apache Shiro™ is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.

Shiro是一款强大易用的Java安全框架,主要功能是进行认证、授权、加密、会话管理等。与Spring Security相比的话,Shiro是会更加轻量。

2 环境搭建

2.1 开发环境

  • IDEA

  • HTML/CSS/JavaScript + FreeMarker

  • JDK8 + Maven + Spring Boot + Shiro

  • druid + mybatis + Navicat + MySQL5.7

这里需要你有预备知识:

  • 熟练使用IDEA或eclipse

  • 了解Maven,知道如何配置settings.xml与maven仓库

  • 了解Spring Boot,并会使用它搭建Web项目

  • 了解FreeMarker/thymeleaf、yaml语法

虽然都是通过Maven构建项目,不过Spring Boot不再使用.xml文件对项目进行配置,所以Shiro的配置类ShiroConfig不太容易理解,对于刚开始接触Spring Boot的同学来说这是一个难点。

2.2 创建项目

这里使用IDEA通过Spring Initializr创建项目(当然也可以通过Maven自己添加所有依赖)。
选择依赖
我们下面要通过这个项目来搭建一个简单的用户系统,系统有两类用户——普通用户和VIP用户,通过shiro来完成登录、登出、权限验证功能,主要在于展示前后端的交互、后端与数据库的交互。

2.3 项目文件结构

项目结构
项目结构2

2.4 pom.xml

新建的项目还需要再手动增加三个依赖项:druid数据库连接池,shiro依赖,log4j依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rhine.blog</groupId>
    <artifactId>shirodemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>shirodemo</name>
    <description>Spring Boot with Shiro</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- Shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.5 application.yml

druid的配置会稍微多些,所以也附上了详细的注释,属性和值之间必须要有":"号。

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/lastpass?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
        
  • 24
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值