Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心

话不多说直接上代码起飞

提示:demo是基于数据库模式搭建,使用内存模式可以看注释的代码,稍加改动就可以实现

pom.xml添加Jar包依赖,目前百度到很多博客写的都是Springboot2.0以下的依赖,2.0以上改动了很多东西,只需要两个Jar包如下:



    org.springframework.boot
    spring-boot-starter-security

 
     org.springframework.security.oauth.boot
     spring-security-oauth2-autoconfigure
     2.0.0.RELEASE
 

一、授权中心需要3个配置类代码如下:

1、OauthServerConfig类

        备注:dataSource,在yml中配置好了之后spring默认会将其注入进来,就是访问数据库的配置,比如mysql数据库的url,用户名,密码就可以了;

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第1张图片

configure(ClientDetailsServiceConfigurer clients)方法内容:

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
        //数据库模式
        clients.jdbc(dataSource)
        //
        //                .withClient("rock")
//                .secret(passwordEncoder.encode("123456"))
//                .authorizedGrantTypes("authorization_code","refresh_token","password","implicit")
//                .scopes("all")
//                .authorities("ROLE_admin","ROLE_user")
//                .redirectUris("http://www.baidu.com")
//                .accessTokenValiditySeconds(120000)
//                .refreshTokenValiditySeconds(50000);
        //
        ;

        //
        //        clients.inMemory()
        //                .withClient("rock")
        //                .secret(passwordEncoder.encode("123456"))
        //                .authorizedGrantTypes("authorization_code","refresh_token","password","implicit")
        //                .scopes("all")
        //                .authorities("ROLE_admin1","ROLE_user")
        //                .redirectUris("https://www.baidu.com")
        //                .accessTokenValiditySeconds(120000)
        //                .refreshTokenValiditySeconds(50000)
        //        ;
        //
    }

configure(AuthorizationServerSecurityConfigurer configurer)方法内容:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第2张图片

configure(AuthorizationServerEndpointsConfigurer endpointsConfigurer)方法内容:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第3张图片

2、 WebSecurityConfig类

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第4张图片

PasswordEncoder myPasswordEncoder()方法内容:

configure(HttpSecurity http)方法内容:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第5张图片

configure(AuthenticationManagerBuilder builder)方法内容:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第6张图片

3、MyPasswordEncoder类,自定义加密方式的类,这里不指定加密方式,直接比较明文

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第7张图片

二、授权中心还需要读取数据库的Service层、Mapper层和Entity实体类,此处只列出Service层和Entity实体的内容

1、UserInfo实体类必须实现OAutch需要的UserDetails接口

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第8张图片

2、UserRole实体类必须实现OAuth需要的GrantedAuthority接口

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第9张图片

3、Service层添加OAuth需要的UserDetailsService接口的实现,UserMapper自行通过mybatis实现;

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第10张图片

三、授权中心需要数据持久化存储需要数据库,此处一Mysql为例,以下是创建数据库表的sql脚本:

备注:用户表(tb_user)需要先添加好用户数据才可以正常使用,密码由于我们使用的是校验明文,所以直接在表字段值填写明文即可,OAuth默认使用到的表是oauth_client_details,其余的表自行决定如何构建权限关系;

 

/*
Navicat MySQL Data Transfer

Source Server         : 192.168.1.200
Source Server Version : 50716
Source Host           : 192.168.1.200:3306
Source Database       : oauth2

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

Date: 2020-06-08 12:16:48
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for oauth_access_token
-- ----------------------------
DROP TABLE IF EXISTS `oauth_access_token`;
CREATE TABLE `oauth_access_token` (
  `token_id` varchar(255) DEFAULT NULL,
  `token` blob,
  `authentication_id` varchar(255) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `client_id` varchar(255) DEFAULT NULL,
  `authentication` blob,
  `refresh_token` varchar(255) DEFAULT NULL,
  KEY `token_id_index` (`token_id`),
  KEY `authentication_id_index` (`authentication_id`),
  KEY `user_name_index` (`user_name`),
  KEY `client_id_index` (`client_id`),
  KEY `refresh_token_index` (`refresh_token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for oauth_client_details
-- ----------------------------
DROP TABLE IF EXISTS `oauth_client_details`;
CREATE TABLE `oauth_client_details` (
  `client_id` varchar(255) NOT NULL,
  `resource_ids` varchar(255) DEFAULT NULL,
  `client_secret` varchar(255) DEFAULT NULL,
  `scope` varchar(255) DEFAULT NULL,
  `authorized_grant_types` varchar(255) DEFAULT NULL,
  `web_server_redirect_uri` varchar(255) DEFAULT NULL,
  `authorities` varchar(255) DEFAULT NULL,
  `access_token_validity` int(11) DEFAULT NULL,
  `refresh_token_validity` int(11) DEFAULT NULL,
  `additional_information` text,
  `autoapprove` varchar(255) DEFAULT 'false',
  PRIMARY KEY (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for oauth_code
-- ----------------------------
DROP TABLE IF EXISTS `oauth_code`;
CREATE TABLE `oauth_code` (
  `code` varchar(255) DEFAULT NULL,
  `authentication` blob,
  KEY `code_index` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for oauth_refresh_token
-- ----------------------------
DROP TABLE IF EXISTS `oauth_refresh_token`;
CREATE TABLE `oauth_refresh_token` (
  `token_id` varchar(255) DEFAULT NULL,
  `token` blob,
  `authentication` blob,
  KEY `token_id_index` (`token_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for tb_permission
-- ----------------------------
DROP TABLE IF EXISTS `tb_permission`;
CREATE TABLE `tb_permission` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `parent_id` bigint(11) DEFAULT NULL COMMENT '用户ID',
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '权限名字',
  `ename` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '权限名字',
  `url` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '请求路径',
  `description` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '描述',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

-- ----------------------------
-- Table structure for tb_role
-- ----------------------------
DROP TABLE IF EXISTS `tb_role`;
CREATE TABLE `tb_role` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `parent_id` bigint(20) DEFAULT NULL COMMENT '父类ID',
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '角色名字',
  `ename` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '角色名字',
  `description` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '描述',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

-- ----------------------------
-- Table structure for tb_role_permission
-- ----------------------------
DROP TABLE IF EXISTS `tb_role_permission`;
CREATE TABLE `tb_role_permission` (
  `id` bigint(11) NOT NULL COMMENT '主键ID',
  `role_id` bigint(11) DEFAULT NULL COMMENT '角色ID',
  `permission_id` bigint(11) DEFAULT NULL COMMENT '权限ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
  `gender` int(2) DEFAULT NULL COMMENT '性别(1男 2女)',
  `email` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '邮箱',
  `create_time` datetime DEFAULT NULL COMMENT '用户创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `removed` int(2) DEFAULT NULL COMMENT '是否删除(1删除0未删除)',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

-- ----------------------------
-- Table structure for tb_user_role
-- ----------------------------
DROP TABLE IF EXISTS `tb_user_role`;
CREATE TABLE `tb_user_role` (
  `id` bigint(11) NOT NULL COMMENT '主键ID',
  `user_id` bigint(11) DEFAULT NULL COMMENT '用户主键',
  `role_id` bigint(11) DEFAULT NULL COMMENT '角色ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
 

 

四、资源服务只需要添加一个配置类即可

 

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第11张图片

 

各个文件请查看相关下载文件

https://download.csdn.net/download/u013327224/12504968

 

password模式:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第12张图片

 

Code模式:

先访问:http://127.0.0.1:18084/oauth/authorize?client_id=rock&response_type=code&redirect_uri=http://www.baidu.com来获取code

然后再如下访问:

Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心_第13张图片

 

你可能感兴趣的:(Springboot 2.0以上版本整合OAuth2.0搭建授权服务中心)