Spring Security OAuth2 Provider 最小实现(Spring Boot 2.1.3)

本文展示了Spring Boot 2.1.3和Spring Security OAuth2的最小实现,涵盖了授权流程中的关键步骤,包括授权码的获取、令牌的申请、API访问以及令牌刷新。在过程中解决了Spring Boot升级后不支持明文密码的问题,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:https://www.iteye.com/blog/rensanning-2384996

在原文链接的指引下,遇到以下问题,已解决把问题展示出来,后面正文的代码都是解决后的代码:

1.第一步授权请求时,后台报java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”,这是由于springboot版本升级导致不支持明文密码,查找资料:在Config.java->SecurityConfig

里添加代码:

@Bean
public PasswordEncoder passwordEncoder() {
	return NoOpPasswordEncoder.getInstance();
}


2.第四步刷新token时,后台报错Handling error: IllegalStateException, UserDetailsService is required.查找资料:在Config.java->SecurityConfig里添加代码:

@Bean
@Override
protected UserDetailsService userDetailsService() {
	return super.userDetailsService();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
	return super.authenticationManagerBean() ;
}

Config.java->OAuthAuthorizationConfig添加代码:

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private AuthenticationManager authenticationManager;
	
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
	endpoints.authenticationManager(authenticationManager)
			.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
			.userDetailsService(userDetailsService);
}


正文开始:Spring Boot 2.1.3、Spring Security OAuth2 2.0.12

OAuth2.0的开源 Server / Client 实现可以参考这里:https://oauth.net/code/,这里采用Spring Security OAuth2实现四种授权模式中最常用的:Authorization Code Grant。


具体可以看OAuth2.0标准的定义:https://tools.ietf.org/html/rfc6749#section-4.1

这里首先只为演示 OAuth2.0 的整个过程,做最小实现!

Spring Security OAuth2默认提供的四个URL:

  • /oauth/authorize : 授权AuthorizationEndpoint
  • /oauth/token : 令牌TokenEndpoint
  • /oauth/check_token : 令牌校验CheckTokenEndpoint
  • /oauth/confirm_access : 授权页面WhitelabelApprovalEndpoint
  • /oauth/error : 错误页面WhitelabelErrorEndpoint


相关文章:
Spring Security OAuth2 Provider 之 最小实现
Spring Security OAuth2 Provider 之 数据库存储
Spring Security OAuth2 Provider 之 第三方登录简单演示
Spring Security OAuth2 Provider 之 自定义开发
Spring Security OAuth2 Provider 之 整合JWT

代码如下:

pom.xml

Xml代码 

<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.security.oauth</groupId>  
    <artifactId>spring-security-oauth2</a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值