Angular集成Spring Boot,Spring Security,JWT和CORS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Angular集成Spring Boot,Spring Security,JWT和CORS相关的知识,希望对你有一定的参考价值。

本文介绍了Spring Boot的基本配置,Angular集成Spring Boot、Spring Security的方法。当前流行的JWT更适合与Angular集成,优于Spring Secuity提供的CSRF。另外引入了springfox-swagger和spring-boot-starter-actuator,演示了如何利用Swagger生成JSON API文档,如何利用Actuator监控应用。

本文前端基于Angular官方样例Tour of Heroes,请先到官网下载。

技术堆栈

Spring Boot 1.5.10.RELEASE
Spring Security
Spring Data
JWT
Swagger
Angular 5.0

Angular 5.0新特性
? 编译性能提升,AOT编译速度加快,已成为开发中推荐的编译方式
? i18n支持新的number、 date、 currency pipes
? 使用StaticInjector替代ReflectiveInjector,不再需要Reflect polyfill,减少app大小。
? exportAs支持多个名字
? 原@angular/http模块已过期,推荐使用HttpClient
? 新增Router Lifecycle Events
GuardsCheckStart, ChildActivationStart, ActivationStart, GuardsCheckEnd, ResolveStart, ResolveEnd, ActivationEnd, ChildActivationEnd
? 语法检查更严格

Spring Boot

创建Spring Boot App

创建Spring Boot项目最简易的方式是使用SPRING INITIALIZR
技术分享图片
输入Group、Artifact,选择Dependency后,点击Generate Project,会生成zip包。下载后解压,编辑POM文件,添加java-jwt和springfox-swagger。完成的POM文件如下:

<?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>

    <groupId>org.itrunner</groupId>
    <artifactId>heroes</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>heroes</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>

    <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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

未完,待续

Angular
Spring Boot
Spring Security
JWT Libraries
Springfox Swagger
Postman
Angular Security - Authentication With JSON Web Tokens (JWT): The Complete Guide
Integrating Angular 2 with Spring Boot, JWT, and CORS, Part 1
Integrating Angular 2 with Spring Boot, JWT, and CORS, Part 2

以上是关于Angular集成Spring Boot,Spring Security,JWT和CORS的主要内容,如果未能解决你的问题,请参考以下文章

集成 Angular + Nodejs + Spring Boot Java REST API 使 Angular Universal 工作

Drools集成SpringBoot

Angular(SPA) 前端和 Spring Boot 后端的 SAML 2.0 集成

关于 Angular 中的 Microsoft Graph API 集成 - Spring Boot 应用程序

Spring Boot 集成 Ehcache 缓存,三步搞定!

开发一个属于自己的Spring Boot Starter