本项目是基于慕课网的Spring Security技术栈开发企业级认证与授权,采用IDEA开发,本文章用来记录该项目的学习过程。

慕课网视频:https://coding.imooc.com/class/134.html

一、项目结构

newbe-security是父类,打包方式是pom,其余都是子模块,打包方式jar.

项目模块介绍:

二、项目依赖

2.1newbe-security父类依赖

<?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>com.newbe.security</groupId>
    <artifactId>newbe-security</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>newbe-security-core</module>
        <module>newbe-security-demo</module>
        <module>newbe-security-browser</module>
        <module>newbe-security-app</module>
    </modules>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.2newbe-security模块依赖

<?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">
    <parent>
        <artifactId>newbe-security</artifactId>
        <groupId>com.newbe.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>newbe-security-core</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- APP安全认证的重要依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>

        <!-- session存储依赖,暂时用不到,先注释掉 -->
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
        <!--</dependency>-->

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

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

        <!-- 第三方登录用到的重要依赖 -->
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-security</artifactId>
        </dependency>

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

        <!-- 工具依赖 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
        </dependency>

    </dependencies>
</project>


2.3newbe-security-browse模块依赖

<?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">
    <parent>
        <artifactId>newbe-security</artifactId>
        <groupId>com.newbe.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>newbe-security-browser</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>Spring Security技术栈开发企业级认证与授权浏览器项目</description>

    <dependencies>
    <dependency>
        <groupId>com.newbe.security</groupId>
        <artifactId>newbe-security-core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <!-- 浏览器端Session管理的重要依赖 -->
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    </dependency>
    </dependencies>
</project>

2.4newbe-security-app模块依赖

<?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">
    <parent>
        <artifactId>newbe-security</artifactId>
        <groupId>com.newbe.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>newbe-security-app</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>Spring Security技术栈开发企业级认证与授权移动端项目</description>

    <dependencies>
        <dependency>
            <groupId>com.newbe.security</groupId>
            <artifactId>newbe-security-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2.5newbe-security-demo模块依赖

<?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">
    <parent>
        <artifactId>newbe-security</artifactId>
        <groupId>com.newbe.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>newbe-security-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>Spring Security技术栈开发企业级认证与授权案例项目</description>

    <dependencies>
        <!-- 浏览器端的安全开发 -->
        <dependency>
            <groupId>com.newbe.security</groupId>
            <artifactId>newbe-security-browser</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <!-- spring boot应用打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.10.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

三、编写Spring Boot启动程序

在demo新建包,新建启动类,配置application.properties

3.1DemoApplication类

package com.newbe.security;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: Newbe
 * @Date: 2019/11/22 00:51
 * @Description:
 */
@SpringBootApplication
@RestController
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
    @GetMapping("/hello")
    public String hello() {
        return "hello spring security";
    }

}

3.2配置application.properties(最初)

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/newbe-demo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 1234567

3.3创建数据库

数据库名newbe-demo,表名User

/*
Navicat MySQL Data Transfer

Source Server         : 127.0.0.1
Source Server Version : 50640
Source Host           : 127.0.0.1:3306
Source Database       : newbe-demo

Target Server Type    : MYSQL
Target Server Version : 50640
File Encoding         : 65001

Date: 2019-11-22 16:55:10
*/

;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `username` ) DEFAULT NULL,
  `password` ) DEFAULT NULL,
  `id` ) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
');
');

3.4启动spring Boot程序

启动 DemoApplication类,控制台报错,原因是newbe-security-core中加入了Spring Session的依赖,没有配置Session的存储方式导致出错

Caused by: java.lang.IllegalArgumentException: No Spring Session store is configured: set the 'spring.session.store-type' property

因此需要配置spring.session.store-type

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/newbe-demo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 1234567

#Spring Session 不配置
spring.session.store-type = none

再次启动DemoApplication类,控制台没有报错启动成功,访问 127.0.0.1:8080/hello,浏览器默认弹出认证窗口,

原因是由于这个org.springframework.cloud的权限依赖包默认这样

<!-- APP安全认证的重要依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>

关闭权限验证

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/newbe-demo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 1234567

#Spring Session 不配置
spring.session.store-type = none

#关闭spring cloud spring-cloud-starter-oauth2权限认证 权限验证关闭
security.basic.enabled=false
最后启动DemoApplication类,再次访问127.0.0.1:8080/hello,浏览器显示如下图

环境配置到此结束!

Spring Security技术栈开发企业级认证与授权(一)环境搭建的更多相关文章

  1. 用“MEAN”技术栈开发web应用(二)express搭建服务端框架

    上一篇我们讲了如何使用angular搭建起项目的前端框架,前端抽象出一个service层来向后端发送请求,后端则返回相应的json数据.本篇我们来介绍一下,如何在nodejs环境下利用express来 ...

  2. Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一)

    标题 Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一) 技术 Spring Boot 2.Spring Security 5.JWT 运行环境 ...

  3. 用“MEAN”技术栈开发web应用(三)用mongodb搭建数据库

    上一篇介绍了如何用express搭建起服务端MVC的开发架构,本篇我们来详细介绍一下这个Model层,也就是数据库访问层.包含如何使用mongodb搭建数据库,以及如何使用mongoose来访问数据. ...

  4. 用“MEAN”技术栈开发web应用(一)AngularJs前端架构

    前言 不知何时突然冒出“MEAN技术栈”这个新词,听起来很牛逼的样子,其实就是我们已经熟悉了的近两年在前端比较流行的技术,mongodb.express.angularjs.nodejs,由于这几项技 ...

  5. “MEAN”技术栈开发web应用

    “MEAN”技术栈开发web应用 上一篇我们讲了如何使用angular搭建起项目的前端框架,前端抽象出一个service层来向后端发送请求,后端则返回相应的json数据.本篇我们来介绍一下,如何在no ...

  6. Spring Security使用数据库数据完成认证--练气后期2

    写在前面 没错,这篇文章还是练气后期!但作者我相信筑基指日可待! 在前一篇文章当中,我们简单地分析了一下Spring Security的认证流程,知道了如果想要实现对自己用户数据(账户.角色.权限)的 ...

  7. Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二)

    Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二) 摘要 上一篇https://javaymw.com/post/59我们已经实现了基本的登录和t ...

  8. [转]GeoServer地图开发解决方案(一):环境搭建篇

    GeoServer 是 OpenGIS Web 服务器规范的 J2EE 实现的社区开源项目,利用 GeoServer 可以方便的发布地图数据,允许用户对特征数据进行更新.删除.插入操作,通过 GeoS ...

  9. OAuth2.0学习(2-1)Spring Security OAuth2.0 开发指南

    开发指南:http://www.cnblogs.com/xingxueliao/p/5911292.html Spring OAuth2.0 提供者实现原理: Spring OAuth2.0提供者实际 ...

随机推荐

  1. 软件工程概论,java web项目

    需要网站系统开发需要掌握的技术: 实施Java的WEB项目需要掌握的技术如:面向对象分析设计思想,设计模式和框架结构,XML语言,网页脚本语言,数据库,应用服务器,集成开发环境Java语言是完全面向对 ...

  2. socket编程执行远程命令实现_python

    一.单客户端连接的例子: 服务端: import socket,os,subprocess server=socket.socket() server.bind(('localhost',1234)) ...

  3. Centos7安装gitlab-ce

    1.官方推荐方式安装 参考https://www.gitlab.com.cn/installation/#centos-7?version=ce sudo yum install -y curl po ...

  4. 微信小程序weui的使用

    大家好,我是前端菜鸟,大家可以叫我惊蛰,今天给大家分享一下在微信小程序中对weui的引入和使用,其他的也不再赘述,文中有不对的还请指正,谢谢. 直入主题: 1.下载weui 进入GitHub http ...

  5. [HNOI2017] 大佬 - DP,BFS,Hash,单调性

    这真的是一道综合题.然而感觉A得莫名其妙,交上去的时候其实非常虚,然后就莫名其妙地AC了? 首先我们考虑到,所有和怼有关的操作都是时刻无关的.也就是说,我们把这些操作拆散放到任何时候都对结果不会有影响 ...

  6. gitlab 更换服务器后访问 Integrations 出现 500 错误

    异常问题解决方案:问题:gitlab 更换服务器后访问 Integrations 出现 500 错误解决方案:从原服务器上将 /etc/gitlab/gitlab-secrets.json 复制过来覆 ...

  7. 使用SVN更新项目后出现冲突说明

    使用Update后出现多个文件,并且报错!!  R.java.mine   R.java.r21965  R.java.r23204 下面以自动生成R.java.mine,R.java.r21965, ...

  8. 为什么 RMAN 控制文件自动备份的名称格式没有遵循 %F 规则

    在 Oracle 中越是简单的问题,往往越难找到答案,举个例子: 你是否留意观察过在 RMAN 进行备份的时候,自动生成的控制文件名称是否是按照 %F 规则来生成的? 关于控制文件自动备份路径格式,在 ...

  9. 【vue】 vue跳转页面:router-link/this.$router.push()

    1.通过标签<router-link> <router-link to='A'>跳转到A页面</router-link> 2.通过方法 this.$router.p ...

  10. Base64编码和其在图片的传输的应用

    Base64 [原文链接] 目前Base64已经成为网络上常见的传输8Bit字节代码的编码方式之一.做支付系统时,系统之间的报文交互都需要使用Base64对明文进行转码,然后再进行签名或加密,之后再进 ...