Spring Boot 整合Quartz、Shiro

Spring Boot 集成Shiro

1. 集成核心内容:
ShiroFilterFactory: shiro过滤器工厂
ShiroFilterFactoryBean implements ShiroFilterFactory,依赖于SecurityManager
SecurityManager:安全管理器
用于身份认证、cookie管理、缓存管理
realm :身份权限信息验证,实现Realm,继承AuthorizingRealm
实现 override doGetAuthenticationInfo()和doGetAuthorizationInfo()
HashCreditialsMatcher:凭证匹配器
默认(storedCredentialsHexEncoded=false)Base64编码,
可以修改为(storedCredentialsHexEncoded=true)Hex编码。
LifecycleBeanPostProcessor:生命周期处理器
开启Shiro的注解功能(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用
Shiro注解的类,并在必要时进行安全逻辑验证,需要配置两个bean(DefaultAdvisorAutoProxyCreator
(可选)和AuthorizationAttributeSourceAdvisor)实现此功能。

 

目录

第1章:Spring Boot入门... 2

1.1 Spring Boot历史背景... 2

1.2 Spring Boot简介... 2

1.3 Spring Boot快速搭建和启动... 3

第2章:Spring Boot基础知识... 7

2.1 统一父pom管理... 7

2.2 Spring Boot启动注解分析... 7

2.3 项目热部署... 8

第3章:Spring Boot配置深入... 8

3.1 配置环境属性... 8

3.1 @SpringBootApplication. 10

3.2 @EnableAutoConfiguration. 10

3.3 @Restcontroller. 10

第4章:数据库操作实现Restful程序... 10

4.1 编写Restful程序,进行项目演示... 10

第5章:Spring Boot整合Mybatis. 11

第6章:Thymeleaf模板引擎... 12

第7章:项目打包... 13

7.1 单一模块项目打包... 13

7.2 多模块化项目管理打包... 14

 


 

Spring Boot框架开发

第1章:Spring Boot入门

1.1 Spring Boot历史背景

l  Java在项目开发中,大量使用maven技术作为开发,项目的部署需要打包成war文件,而后上传到系统中,在部署上很麻烦。

l  在使用spring进行开发中,需要配置一堆的依赖库。

l  在Restful技术中,实现Rest架构的开发,也是很麻烦。

l  在Spring中集成mysql,shiro,Redis等,都需要编写一堆的.xml文件

为了解决上面一系列的问题,Spring Boot产生。

1.2 Spring Boot简介

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.

Spring Boot可以非常容易的创建一个独立的、基于生产的spring 应用程序,我们对于spring和第三方类库要了解的话,这样可以很方便的开始Spring Boot,大多数的Spring Boot 都需要一点Sping的配置。

l  Spring Boot是Spring推出的一种微服务架构的开发框架,它可以减少复杂的配置文件,让配置、部署、监控更简单。

l  其中使用的注解大多数都是spring的。

微服务概念和特性:

  • • 一种软件架构模式 
    • 复杂应用解耦为小而众的服务 
    • 各服务精而专 
    • 服务间通信通过API完成

微服务应用的一个最大的优点是,它们往往比传统的应用程序更有效地利用计算资源。这是因为它们通过扩展组件来处理功能瓶颈问题。这样一来,开发人员只需要为额外的组件部署计算资源,而不需要部署一个完整的应用程序的全新迭代。最终的结果是有更多的资源可以提供给其它任务。

单体架构所有的模块都共用一个数据库,存储方式比较单一,微服务每个模块都可以使用不同的存储方式(比如有的用redis,有的用mysql等),数据库也是单个模块对应自己的数据库。

1.3 Spring Boot快速搭建和启动

         创建Spring Boot的项目方式有很多,这里只说明三种方式。

注:版本支持jdk1.8,maven3.2以上

1.3.1 通过创建Maven项目

1. 创建Maven Project项目

 

2 要开发Spring Boot项目,按照官方的要求配置一个父pom,在pom.xml文件中添加如下代码:

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.10.RELEASE</version>

</parent>

3 修改jdk的版本,添加maven编译器插件更新版本。

        <build>

               <plugins>

                       <plugin>

                               <groupId>org.apache.maven.plugins</groupId>

                               <artifactId>maven-compiler-plugin</artifactId>

                               <configuration>

                                      <source>{jdk.version}</source>

                                      <target>{jdk.version}</target>

                                      <encode>{project.build.sourceEncoding}</encode>

                               </configuration>

                       </plugin>

               </plugins>

        </build>

4 添加如下节点后,右键更新maven项目update project

<properties>

        <jdk.version>1.8</jdk.version>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

5 添加spring boot项目依赖包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

6 修改java compiler版本1.8

 

7 至此,项目搭建成功,可以直接右键运行java application程序,启动。

 

1.3.2 使用start.spring.io

         Spring Boot提供Spring Initializr可以快速创建项目,步骤如下:

1、在浏览器中输入start.spring.io,输入Group、Artifact,Search for dependencies,这里只选择Reactive Web,然后下载即可,如图。

 

2、导入到开发环境Eclipse中

3、右键选择maven -> update project,下载依赖包,更新项目,如图所示。

 

1.3.3 使用dos命令

1、创建并选择项目存放的位置。

2、输入cmd进入到项目目录,输入 mvn archetype:generate -Dinteractivemodel=false -DgroupId=com.immoc -DartifactId=first-app-by-maven -Dversion=1.0.0-SNAPSHOT。其中,-DgroupId、-DartifactId可以自己随意输入

3、将生成的项目导入到maven中,手动创建相应的src/main/java、src/main/resource、src/main/test等目录

4、根据官网提供的QUICK START实例,配置pom节点,然后更新maven项目。

5、进入到项目目录,通过mvn install 进行项目编译。

6、在target目录下,会自动生成项目的*.jar文件,输入命令 java -jar *.jar 即可启动

另一中启动方式,进入到项目的目录下,输入命令mvn spring-boot:run运行启动项目。

第2章:Spring Boot基础知识

2.1 统一父pom管理

1、首先建立一个Microboot的Maven项目,修改packaging为pom,并添加dependencyManagement依赖管理器。

2、修改jdk的版本

3、添加spring boot-dependencies依赖。

<dependencyManagement>

      <dependencies>

      <dependency>

        <!-- Import dependency management from Spring Boot -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-dependencies</artifactId>

        <version>2.0.0.RC1</version>

        <type>pom</type>

           <scope>import</scope>

      </dependency>

   </dependencies>

</dependencyManagement>

4、按照同样的方法建立子模块microboot-base。
5、导入Spring Boot Start web启动包。

2.2 Spring Boot启动注解分析

1、@EnableAutoConfiguration: 开启自动配置处理,它需要跟着程序的主类一起定义。

 

@EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

这个注解告诉spring是基于jar依赖包配置spring,因为web启动包添加tomcat和springmvc,自动配置猜想你正开发一个带有spring的 web应用程序。

2.3 项目热部署

在pom.xml文件中添加如下依赖:

<dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <optional>true</optional>

</dependency>

<dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>springloaded</artifactId>

        <optional>true</optional>

 </dependency>

第3章:Spring Boot配置深入

3.1 配置环境属性

参考MyFirstSpringBoot/HelloController

1、   端口号、访问路径的修改

方式一: 在application.properties文件中添加如下属性配置。

server.port=8081
server.context-path=/renren

方式二: 使用YAML文件代替属性配置文件。新建application.yml文件,在文件中输入如下代码,注意:属性名和属性值前后必须要有空格。

server:

post: 8081

context-path: /renren

2、   属性中使用占位符

app.name=MyApp

app.description=${app.name} is a Spring Boot application

3、   单个读取配置文件的属性

@Value :  @Value(“${param}”),单个读取属性值

4、   配置文件中使用配置文件

${}

5、   使用Bean的方式读取配置文件

@Component

@ConfigurationProperties(prefix=”groupId”),读取多个属性值

6、   多环境配置

         创建dev、prod两个yml文件,例如:修改为不同的端口号等

通过配置spring.profiles.active=dev,来实现,为了在不同的环境下测试,要先通过mvn install进行编译生成*.jar文件,然后进入target目录,输入jar –jar *.jar --spring.profiles.active=dev来测试不同的环境。

7、   修改默认图案Banner

l  自定义Banner

方法一:在classpath目录下添加banner.txt.

方法二:在classpath下创建一个配置文件,设置spring.banner.location,spring.banner.charset,banner.gif/.jpg/.png,

spring.banner.image.location

2、关闭Banner

SpringApplication还有一个实例方法run(上面的run是静态方法,这个类一共有四个run方法,其中2个是静态的)和另一个实例方法void setShowBanner(boolean showBanner), which is我们正想用的。所以修改代码如下:

public static void main(String[] args) {

SpringApplication application = new SpringApplication(MyFirstSpringBootApplication.class); 

application.setBannerMode(Mode.OFF);

 application.run(args);

         }

}

3.1 @SpringBootApplication

package com.example.myproject;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3.2 @EnableAutoConfiguration

1、开启自动配置类。启动后会默认自动加载很多的类,如果不想加载,可以使用@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})排除

2、如果class不在classpath下,可以通过excludeName 属性去改变排除

3.3 @Restcontroller

演示controller 和 RestController不同。

         1、添加spring –boot-starter-thymeleaf

         2、创建templates文件。

第4章:数据库操作实现Restful程序

4.1 编写Restful程序,进行项目演示

这里说明spring官方提供的spring-data-jpa。

1、  添加依赖类库spring-boot-starter-data-jpa。

<dependency>

   <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-data-jpa</artifactId>

 </dependency>

2、  添加mysql类库。

<dependency>

    <groupId>mysql</groupId>

     <artifactId>mysql-connector-java</artifactId>

  </dependency>

3、  在yml配置文件配置数据库连接相关信息。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url= jdbc:mysql://127.0.0.1:3306/test

spring.datasource.username=root

spring.datasource.password= 123456

4、  这里演示hibernate中ddl-auto的none、create、update、create-drop等值不同之处。

spring:

  profiles:

    active: dev

   

  datasource:

    driver-class-name: com.mysql.jdbc.Driver

    url: jdbc:mysql://127.0.0.1:3306/test

    username: root

    password: 123456

   

  jpa:

    hibernate:

      ddl-auto: update

  show-sql: true

第5章:Spring Boot整合Mybatis

参考demo项目(Address)

1、创建带有mybatis的web项目。

2、创建mapper的映射文件CityMapper.xml

3、编写自己的controller、service、dao等

4、配置mapper映射文件。

5、在yml文件配置加载mybatis映射文件、别名等

6、使用@Mapper在mapper接口上添加mapper,或者是在入口程序中使用@MapperScan("com.example.demo.dao") 配置组件扫描包路径。

插入:

@param 注解:

1、  如果dao方法中有注解,则mapper.xml文件中只能使用注解名访问。

2、  如果dao方法没有注解,判断形参个数:

u  只有一个形参:可以用#{0},#{随意命名}

u  多个形参:只能用#{0},#{1}

第6章:Thymeleaf模板引擎

参考demo/User案例

1、  常用标签使用

th:text: 读取model中的文本值,用于文本显示。

Th:href链接地址。一般写法为th:href="@{‘字符串’+ ${model中的name属性值}}"

th:src: 用于外部资源引入,类似于<script>标签的src属性,常与@{}一起使用。

th:action

定义后台控制器路径,类似<form>标签的action属性。例如:

<form id="login-form"  th:action="@{/login}">...</form>

th:each对象遍历,功能类似jstl中的<c:forEach>标签。

th:iddiv id声明,类似html标签中的id属性

th:if

th:include

th:replace

th:fragment声明定义该属性的div为模板片段,常用与头文件、页尾文件的引入。常与th:include,th:replace一起使用。

<div th: fragment=" copy" >© 2011 The Good Thymes Virtual Grocery</div>

引入模板片段

<div th: include=" /templates/footer : : copy" ></div>
<div th: replace=" /templates/footer : : copy" ></div>

th:object用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数。常与th:field一起使用进行表单数据绑定。

th:field常用于表单字段绑定。通常与th:object一起使用。 属性绑定、集合绑定。

th:src用于外部资源引入,类似于<script>标签的src属性,常与@{}一起使用。

th:text文本显示。例如:

<td class="text" th:text="${username}" ></td>

th:value用于标签复制,类似<option>标签的value属性。例如:

<option th:value="Adult">Adult</option>
<input  id="msg" type="hidden" th:value="${msg}" />

2、  表达式

#{} : Message 表达式

<p th:utext="#{home.welcome(${session.user.name})}"> Welcome to our grocery </p>

${}: 变量表达式

*{} :选择变量表达式

<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>

@{}: 链接 URL 表达式

<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

第7章:项目打包

7.1 单一模块项目打包

1、进入到项目目录,进行项目编译mvn build。

2、通过mvn install 进行项目打包,在target目录中会自动生成项目的*.jar文件,

3、在target目录,生成jar文件,可以输入命令 java -jar *.jar 即可启动。

这里命令可以带参数,针对多环境配置,当有开发环境和生产环境时,为了方便可以, java -jar *.jar  --spring.profiles.active=prod(dev、test)

7.2 多模块化项目管理打包

1、在启动类中修改pom文件(也就是web层的)

<build>
    <!-- 为jar包取名 -->
    <finalName>blm-start</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.0.RELEASE</version>
        </plugin>
</plugins>
</build>

2、在父pom.xml文件中构建插件, 添加<configuration><main-class>

<build>
    <plugins>
        <plugin>
            <!-- The plugin rewrites your manifest -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.0.RELEASE</version>
            <configuration><!-- 指定该Main Class为全局的唯一入口 -->
                <mainClass>com.Application</mainClass>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                    </goals>
                    <!--可以生成不含依赖包的不可执行Jar包-->
                    <!-- configuration>
                      <classifier>exec</classifier>
                    </configuration> -->
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3、输入mvn –Dmaven.test.skip  –u clean package/install

4、进入web工程的target目录中可以看到打包后的文件。

Java –jar *.jar

mvn spring-boot:run

posted @ 2018-01-26 15:44  无敌多么,无敌寂寞  阅读(402)  评论(0编辑  收藏  举报