Spring Authorization Server 登录成功跳转不到认证页面

	最近在学习oauth2的最新版Spring Authorization Server,用官网的demo来运行。
	官方地址:https://github.com/spring-projects/spring-authorization-server/blob/1.0.2/samples/default-authorizationserver。
  	== 发现的问题==
	发现登录成功后就抛错,不能跳转到授权页面。

解决方案

client端添加如下的依赖:

<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
</dependency>

看源码分析

1.登录后都会被OAuth2LoginAuthenticationFilter这类拦截,然后debug到authenticate这个方法。
在这里插入图片描述
最终走到OAuth2LoginAuthenticationProvider的authenticate方法。
在这里插入图片描述
在这里我们发现我们的scope里面是有openID的。这是一个Openid Connext Request,因此这里返回null,让OidcAuthorizationCodeAuthenticationProvider来处理,然后发现缺少spring-security-oauth2-jose这个依赖。添加上以后可以正常跳转到你想访问的页面。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Authorization Server允许我们自定义登录方式。下面是一个示例: 首先,我们需要创建一个自定义的认证提供者(AuthenticationProvider),用于处理自定义登录方式。在这个认证提供者中,我们可以实现自己的认证逻辑。 ```java public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { // 自定义认证逻辑 // ... return new UsernamePasswordAuthenticationToken(username, password, authorities); } @Override public boolean supports(Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } } ``` 然后,我们需要将自定义的认证提供者添加到Spring Security的配置中。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationProvider customAuthenticationProvider; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(customAuthenticationProvider); } // 其他配置... } ``` 最后,我们可以在Spring Authorization Server的配置中使用自定义的认证方式。 ```java @Configuration public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret("secret") .authorizedGrantTypes("custom_grant_type") .scopes("read", "write") .accessTokenValiditySeconds(3600); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } // 其他配置... } ``` 在上面的示例中,我们通过在`configure(ClientDetailsServiceConfigurer clients)`方法中指定`authorizedGrantTypes("custom_grant_type")`来定义了一个自定义的授权方式。然后,在`configure(AuthorizationServerEndpointsConfigurer endpoints)`方法中,我们将`authenticationManager`设置为`AuthorizationServerEndpointsConfigurer`的属性,以便使用自定义的认证方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值