原文链接: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