如何解决最新的春季启动版本未提供oAuth2授权服务器的登录屏幕
我们正在尝试使用Spring Boot使用oAuth2构建自定义身份验证服务器,并由多个客户端使用OAuth2Sso进行访问。相同的代码适用于Spring Boot 1.5.6.RELEASE版本,但不适用于2.3.1.RELEASE。身份验证握手未显示登录屏幕。
ResourceServerConfig代码如下
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationManager getAuthenticationManager() throws Exception {
return authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers("/login","/oauth/authorize")
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.parentAuthenticationManager(authenticationManagerBean())
.inMemoryAuthentication()
.withUser("admin")
.password("admin")
.roles("USER");
}
@Bean
public PasswordEncoder getPasswordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
}
并且AuthorizationServerConfig如下
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("ClientId")
.secret("secret")
.authorizedGrantTypes("authorization_code")
.scopes("user_info")
.autoApprove(true);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
当客户端尝试访问任何安全页面时,它将重定向到oAuth服务器,但未在屏幕上加载登录。错误信息如下所示。
<oauth>
<error_description>Full authentication is required to access this resource</error_description>
<error>unauthorized</error>
</oauth>
如果我们在ResourceServerConfig类中添加@Order(1),则将出现登录屏幕,但在成功进行身份验证之后,它将不会重定向回去。相反,它显示一个错误
,但相同的工作流程适用于上述较早的Spring Boot版本,没有任何更改。请告诉我,Spring Security 5的框架有什么变化,而获得最新版本行为的补救措施是什么。
解决方法
Spring security 5到目前为止还不支持Authorization Server(版本5.3.3.RELEASE) https://spring.io/blog/2019/11/14/spring-security-oauth-2-0-roadmap-update
但是Spring Security宣布了一个社区驱动的项目 https://spring.io/blog/2020/04/15/announcing-the-spring-authorization-server
就目前而言,在资源服务器中使用Spring Security 5,您可以使用外部授权服务器,也可以使用Spring Security oauth 2.3+配置自己的独立Spring授权服务器之一 https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Features-Matrix
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。