SpingSecurityOauth2实现了Oatuh2,SpingSecurityOauth2分为两个大块,一者为认证授权(Authorization Server)服务和资源服务(Resource server),认证授权服务一般负责执行认证逻辑(登录)和加载用户的权限(给用户授权),以及认证成功后的令牌颁发 ,而资源服务器一般指的是我们系统中的微服务(被访问的微服务),在资源服务器需要对用户的令牌(认证成功与否),以及授权(是不是有访问权限)做检查 。
1.2.微服务Oauth2认证解决方案这里我们需要准备四个服务,1.授权服务 2.资源服务,3.网关,4注册中心,一共4个服务。授权服务和资源服务的流程图:
请求流程是:
- 浏览器向认证服务提交认证获取Token
- 浏览器存储Token并向资源服务器发起请求
- 资源服务器校验Token并对资源进行授权
注意:没有SpringCloud基础的同学先去学SpringCloud,搭建基本项目结构如下
security-parent //父工程
security-auth-server //统一认证微服务
security-eureka-server //注册中心
security-resource-server //资源微服务
security-zuul-server //网关
2.1.搭建父工程
管理SpringBoot和SpringCloud相关依赖,管理公共依赖,以及依赖版本统一管理
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.SR1
pom
import
2.2搭建EurekaServer注册中心
1.导入依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
2.启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
3.yml配置
server:
port: 1000
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #禁用注册中心向自己注册
fetchRegistry: false #不让注册中心获取服务的注册列表
serviceUrl:
defaultZone: http://localhost:1000/eureka/
#注册中心的注册地址 ,其他微服务需要向这个地址注册
2.3.搭建ZuulServer网关微服务
微服务统一访问入口,后面实现统一鉴权操作。 1.导入依赖
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.启动类
@SpringBootApplication
@EnableZuulProxy
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
3.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/ #注册中心地址
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: zuul-server:2000 #指定服务的id
server:
port: 2000
zuul:
ignored-services: "*" #禁止使用服务名字进行访问
prefix: "/servers" #统一的前缀
routes: #配置路由,指定服务的访问路径
resource1-server: "/resources/**"
spring:
application:
name: zuul-server
2.4.搭建AuthServer认证授权微服务
认证微服务,实现认证逻辑,用户授权,令牌颁发等 1.导入依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
2.启动类
@SpringBootApplication
public class AuthServerApplication {
public static void main(String[] args) {
SpringApplication.run(AuthServerApplication.class) ;
}
}
3.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/ #注册中心地址
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: auth-server:3000 #指定服务的id
server:
port: 3000
spring:
application:
name: auth-server
2.5.搭建Resource1Server资源微服务
资源服务,负责校验Token和资源授权 1.导入依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
2.启动类
@SpringBootApplication
public class Resource1ServerApplication {
public static void main(String[] args) {
SpringApplication.run(Resource1ServerApplication.class) ;
}
}
3.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/ #注册中心地址
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: resource1-server:4000 #指定服务的id
server:
port: 4000
spring:
application:
name: resource1-server
启动测试http://localhost:1000/
修改security-auth-server服务,完成User,Role,Permission实体类的创建,以及mapper映射器,Mapper.xml等相关组件的创建 , 以及相关表的创建和关系维护,这里省略…
3.2.集成MyBatis1.security-auth-server增加依赖,"spring-cloud-starter-oauth2"包含了Oauth2和Security的内容
org.springframework.cloud
spring-cloud-starter-oauth2
com.alibaba
druid
1.1.20
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
2.开启Mapper扫描"MapperScan"
@SpringBootApplication
@MapperScan("cn.itsource.mapper")
public class AuthServerApplication {
public static void main(String[] args) {
SpringApplication.run(AuthServerApplication.class) ;
}
}
3.yml增加DataSource和MyBatis配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/ #注册中心地址
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: auth-server:3000 #指定服务的id
server:
port: 3000
mybatis:
mapper-locations: classpath:cn/itsource/mapper/*Mapper.xml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: admin
url: jdbc:mysql:///auth-rbac
driver-class-name: com.mysql.jdbc.Driver
application:
name: auth-server
3.3.配置UserDetailsService
定义一个类,实现UserDetailsService接口,复写loadUserByUsername,根据用户名从数据库中获取认证的用户对象,并且加载用户的权限信息,把用户的认证信息和权限信息封装成UserDetaials返回。 在前面的章节已经学习过UserDetailsService的配置这里不在赘述配置如下
package cn.itsource.userdetails;
import cn.itsource.domain.Permission;
import cn.itsource.domain.User;
import cn.itsource.mapper.PermissionMapper;
import cn.itsource.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Component
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Autowired
private PermissionMapper permissionMapper;
/**
* 加载数据库中的认证的用户的信息:用户名,密码,用户的权限列表
* @param username: 该方法把username传入进来,
我们通过username查询用户的信息(密码,权限列表等)
然后封装成 UserDetails进行返回 ,交给security 。
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//User是security内部的对象,UserDetails的实现类 ,
//用来封装用户的基本信息(用户名,密码,权限列表)
//public User(String username, String password, Collection
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?