Spring Security + OAuth2 + JWT 基本使用

前面学习了 Spring Security 入门,现在搭配 oauth2 + JWT 进行测试。

1、什么是 OAuth2

OAuth 是一个关于授权(authorization)的开放网络标准,使得第三方应用可以使用该令牌在限定时间限定范围访问指定资源。在全世界得到广泛应用,目前的版本是2.0版。

1.1、关于 OAuth2 的几个重要概念:

  • resource owner: 拥有被访问资源的用户
  • user-agent: 一般来说就是浏览器
  • client: 第三方应用
  • Authorization server: 认证服务器,用来进行用户认证并颁发token
  • Resource server:资源服务器,拥有被访问资源的服务器,需要通过token来确定是否有权限访问

1.2、握手流程

明确概念后,就可以看 OAuth2 的协议握手流程,摘自RFC6749

(A)用户打开客户端以后,客户端要求用户给予授权。

(B)用户同意给予客户端授权。

(C)客户端使用上一步获得的授权,向认证服务器申请令牌。

(D)认证服务器对客户端进行认证以后,确认无误,同意发放令牌。

(E)客户端使用令牌,向资源服务器申请获取资源。

(F)资源服务器确认令牌无误,同意向客户端开放资源

1.3、授权模式

oauth2根据使用场景不同,分成了4种模式

  • 授权码模式(authorization code)
  • 简化模式(implicit)
  • 密码模式(resource owner password credentials)
  • 客户端模式(client credentials)

授权码模式使用到了回调地址,是最为复杂的方式,通常网站中经常出现的微博,qq第三方登录,都会采用这个形式。简化模式不常用。

2、配置

使用oauth2保护你的应用,可以分为简易的分为三个步骤

  • 配置资源服务器
  • 配置授权服务器
  • 配置spring security

2.1、maven 依赖配置

这里直接引入 spring-cloud oauth2,更加方便之后的拓展。

<!--spring boot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<!--spring cloud oauth2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<!--spring cloud security-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency> <!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies> <!--spring cloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2.2、配置授权服务器

这里需要进行访问客户端的配置,并配置授权类型和access_tokenjwtToken

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailServiceImpl userDetailService;
@Autowired
@Qualifier("jwtTokenStore")
private TokenStore tokenStore;
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired
private JwtTokenEnhancer jwtTokenEnhancer; /**
* 配置授权类型
*
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//设置Jwt内容增强
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
List<TokenEnhancer> list = new ArrayList<>();
list.add(jwtTokenEnhancer);
list.add(jwtAccessTokenConverter);
tokenEnhancerChain.setTokenEnhancers(list); endpoints
//密码模式必须配置
.authenticationManager(authenticationManager)
//密码模式必须配置
.userDetailsService(userDetailService)
//accessToken转JwtToken
.tokenStore(tokenStore)
.accessTokenConverter(jwtAccessTokenConverter)
//jwt内容增强
.tokenEnhancer(tokenEnhancerChain);
} /**
* 配置客户端详情信息
*
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.
//基于内存配置
inMemory()
//客户端ID
.withClient("client")
//密钥
.secret(bCryptPasswordEncoder.encode("112233"))
//重定向地址
.redirectUris("http://www.baidu.com")
//授权范围
.scopes("all")
//accessToken有效时间
.accessTokenValiditySeconds(60)
//refreshToken有效时间
.refreshTokenValiditySeconds(3600)
/**
* 授权类型
* authorization_code:授权码模式
* password:密码模式
* refresh_token:刷新令牌
*/
.authorizedGrantTypes("authorization_code", "password", "refresh_token");
}
}

2.3、配置资源服务器

继承 ResourceServerConfigurerAdapter并添加 @EnableResourceServer注解

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//拦截所有请求
.anyRequest()
.authenticated()
.and()
//spring secuity提供了requestMatchers接口,等价于http.authorizeRequests().anyRequest().access("permitAll");
//提供资源,访问/user需要权限认证
.requestMatchers()
.antMatchers("/user/**");
}
}

2.4、JWT 配置

2.4.1、accessToken 转 JwtToken 配置类

主要工作是创建 JwtAccessTokenConverter并设置密钥,并注入到 Bean 管理容器中。

/**
* accessToken转JwtToken配置
*/
@Configuration
public class JwtTokenStoreConfig { @Bean
public JwtTokenStore jwtTokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
} @Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//设置jwt密钥
jwtAccessTokenConverter.setSigningKey("test_key");
return jwtAccessTokenConverter;
} @Bean
public JwtTokenEnhancer jwtTokenEnhancer() {
return new JwtTokenEnhancer();
}
}
2.4.2、JwtToken内容拓展配置类

当 accessToken 转 jwtToken时,如果想往令牌中加入自定义用户信息,例如登录时间点,可以配置以下类:

/**
* JwtToken内容拓展配置类
* @author Lin
*/
public class JwtTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
Map<String, Object> map = new HashMap<>();
map.put("enhance", "enhance info");
((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(map);
return oAuth2AccessToken;
}
}

2.5、配置 spring security

/**
* spring security配置类
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { /**
* 密码加密
*
* @return
*/
@Bean
public BCryptPasswordEncoder getPasswordEncode() {
return new BCryptPasswordEncoder();
} /**
* 接口请求授权
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/**", "/login/**","/logout/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll()
.and()
.csrf().disable();
} @Override
@Bean
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
}

2.6、实现 UserDetailsService

实现 UserDetailService 用于登录验证,以及密码模式下需要用到。

@Service
public class UserDetailServiceImpl implements UserDetailsService { @Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder; @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
String password = bCryptPasswordEncoder.encode("123456");
return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList("permission1"));
}
}

创建 User实体类如下(非必须):

public class User implements UserDetails {

    private String username;
private String password;
private List<GrantedAuthority> authorities; public User(String username, String password, List<GrantedAuthority> authorities) {
this.username = username;
this.password = password;
this.authorities = authorities;
} @Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
} @Override
public String getPassword() {
return password;
} @Override
public String getUsername() {
return username;
} @Override
public boolean isAccountNonExpired() {
return true;
} @Override
public boolean isAccountNonLocked() {
return true;
} @Override
public boolean isCredentialsNonExpired() {
return true;
} @Override
public boolean isEnabled() {
return true;
}
}

3、运行测试

3.1、获取授权码

直接访问 /oauth/authorize? 接口可以获得授权码

在我的项目中访问路径如下:

http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://www.baidu.com&scope=all

浏览器访问,跳转到http://localhost:8080/login.html默认登录页,点击登录,授权:

跳转到https://www.baidu.com/?code=XKee3V页面,XKee3v就是获得的授权码。

3.2、根据授权码模式获得令牌

利用 postman 测试,访问

http://localhost:8080/oauth/token

配置 Authorization 信息,即登录客户端的账号和密码;

配置 Body 信息,grant_type的参数值是 authorization_code,authorization_code即为授权码模式,code即为上文获得的授权码。

配置完后运行测试,返回 access_tokenrefresh_token,看到 access_token成功转为JwtToken

3.3、密码模式

密码模式比授权码模式简单一点,不需要获得授权码,直接忽略上文获取授权码的操作,只需稍微改动配置信息。

Authorization 信息无需改动,修改 Body 信息, grant_type的参数值改为 password,代表密码模式,填写登录 spring security 的账号和密码。

3.4、刷新令牌

在上文中我设置了 access_token的时效性为60秒,当access_token失效时,需要根据refresh_token获取新的令牌。

访问路径如下:

http://localhost:8080/oauth/token

Authorization 配置信息如下:

Body 需要配置 grant_type的参数值为 refresh_token,代表刷新令牌,并填写refresh_token的参数值。访问后即可获得新的 access_token

3.5、根据 access_token获得资源

访问路径如下:

http://localhost:8080/user/getCurrentUser

Header 请求头添加 Authorization 参数,并设置参数值为 bearer+空格+ access_token,即可获得接口返回值。

4、参考资料

哔哩哔哩 Spring Security教程

阮一峰-OAuth2.0

Spring Security + OAuth2 + JWT 基本使用的更多相关文章

  1. 【Spring Cloud & Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权

    一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证授权.鉴权的逻辑,结合 ...

  2. spring security oauth2 jwt 认证和资源分离的配置文件(java类配置版)

    最近再学习spring security oauth2.下载了官方的例子sparklr2和tonr2进行学习.但是例子里包含的东西太多,不知道最简单最主要的配置有哪些.所以决定自己尝试搭建简单版本的例 ...

  3. [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权

    一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...

  4. Spring Cloud 学习 (十) Spring Security, OAuth2, JWT

    通过 Spring Security + OAuth2 认证和鉴权,每次请求都需要经过 OAuth Server 验证当前 token 的合法性,并且需要查询该 token 对应的用户权限,在高并发场 ...

  5. SpringBoot 整合 spring security oauth2 jwt完整示例 附源码

    废话不说直接进入主题(假设您已对spring security.oauth2.jwt技术的了解,不懂的自行搜索了解) 依赖版本 springboot 2.1.5.RELEASE spring-secu ...

  6. Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案

    一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...

  7. Spring Cloud实战 | 最八篇:Spring Cloud +Spring Security OAuth2+ Axios前后端分离模式下无感刷新实现JWT续期

    一. 前言 记得上一篇Spring Cloud的文章关于如何使JWT失效进行了理论结合代码实践的说明,想当然的以为那篇会是基于Spring Cloud统一认证架构系列的最终篇.但关于JWT另外还有一个 ...

  8. Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

    本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...

  9. Spring Boot 中使用 Spring Security, OAuth2 跨域问题 (自己挖的坑)

    使用 Spring Boot 开发 API 使用 Spring Security + OAuth2 + JWT 鉴权,已经在 Controller 配置允许跨域: @RestController @C ...

随机推荐

  1. GoPath模式和GoMoudle模式的相爱相杀

    相信看我文章的文章的童鞋,golang版本已经是1.3版本以上.如果你的版本还停留在1.3以下,那这篇文章可以做为你的提升之法. go moudle的前世今生 前世-gopath gopath是什么 ...

  2. PTA 两个有序链表序列的合并

    6-5 两个有序链表序列的合并 (15 分)   本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L1, List L ...

  3. 前瞻|Amundsen的数据血缘功能

    目前,Amundsen并不支持表级别和列级别的数据血缘功能,也没有办法展示数据的来龙去脉. 作为Amundsen一项非常核心的功能,Lineage功能早已经提上日程,并进入设计与研发阶段.本位将展示此 ...

  4. 对象存储服务-Minio

    Mino 目录 Mino 对象存储服务 Minio 参考 Minio 架构 为什么要用 Minio 存储机制 纠删码 MinIO概念 部署 单机部署: Docker 部署Minio 分布式Minio ...

  5. Java进阶专题(二十七) 将近2万字的Dubbo原理解析,彻底搞懂dubbo (下)

    ...接上文 服务发现 服务发现流程 整体duubo的服务消费原理 Dubbo 框架做服务消费也分为两大部分 , 第一步通过持有远程服务实例生成Invoker,这个Invoker 在客户端是核心的远程 ...

  6. 201871030118-雷云云 实验二 个人项目—D{0-1}背包问题项目报告

    项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业链接 我的课程学习目标 1.了解并掌握psp2.掌握软件项目个人开发流程3.掌握Github发布软件项目的操作方法 这个作业在哪些方面帮助 ...

  7. oo第三单元——社交网络

    第三单元的作业背景是实现一个社交观关系模拟系统,主要训练了JML的阅读和理解能力,和图的一些数据结构和算法. JML语言的理论基础 JML相对于实现代码是比较抽象的,规定了方法的前提副作用结果.数据的 ...

  8. 消息中间件-RabbitMQ基本使用

    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的.所有主要 ...

  9. 05_pytorch的Tensor操作

    05_pytorch的Tensor操作 目录 一.引言 二.tensor的基础操作 2.1 创建tensor 2.2 常用tensor操作 2.2.1 调整tensor的形状 2.2.2 添加或压缩t ...

  10. Android+Java Web+MySQL实现登录注册

    1 前言&概述 这篇文章是基于此处文章的更新,更新了一些技术栈,更加贴近实际需要,以及修复了若干的错误. 这是一个前端Android+后端Java/Kotlin通过Servelt进行后台数据库 ...