springcloud oauth2 实现单点登录时 login 循环重定向问题

在实现SpringCloudOAuth2单点登录时遇到循环重定向问题,经过排查发现是cookie.name设置问题导致。调整后,虽然仍循环重定向但出现403错误,进一步测试发现是check_token接口返回403。参考博客解决了问题。

最近实践了spring cloud oauth2实现单点登陆,在认证服务器验证了通过code获取token接口,refresh token接口,然后就去搞client了。

结果就发现,访问client资源,自动跳转到server登陆页,登陆之后跳转client的/login,携带了code,然后又重定向到server的/login,然后再次登陆,然后就一直这么死循环的重定向/login。

百思不得其解,各种网上找资料,终于然我发现了一个问题,cookie.name

附上博客链接https://blog.youkuaiyun.com/Mr_XiMu/article/details/106461106

设置了client的cookie.name,虽然还是一直循环重定向,但是此时后台报错了,喜讯啊报错就是喜讯,client在进行check_token时403了,回头再单独测试一下server的check_token接口,果然还是403,于是沿着上面博文找到了博主的认证服务器篇https://blog.youkuaiyun.com/mr_ximu/article/details/106269071

增加如下代码,完美解决

在此感谢博主@西木Qi

附上博主的博文SpringBoot集成SpringSecurity5和OAuth2 总结

package com.zjl.oauth.secuity.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; import org.springframework.security.oauth2.provider.ClientDetailsService; import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenStore; import java.util.Base64; import java.util.concurrent.TimeUnit; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; import javax.sql.DataSource; /** * 数据库授权服务器 * @author zjl * @date 2023/7/13 */ @Configuration @EnableAuthorizationServer public class JdbcAuthorizationServer extends AuthorizationServerConfigurerAdapter { private final AuthenticationManager authenticationManager; private final PasswordEncoder passwordEncoder; private final DataSource dataSource; @Autowired public JdbcAuthorizationServer(AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder, DataSource dataSource) { this.authenticationManager = authenticationManager; this.passwordEncoder = passwordEncoder; this.dataSource = dataSource; } @Bean // 声明TokenStore实现 public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean // 声明 ClientDetails实现 public ClientDetailsService clientDetails() { JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource); jdbcClientDetailsService.setPasswordEncoder(passwordEncoder); return jdbcClientDetailsService; } @Override //配置使用数据库实现 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager);//认证管理器 endpoints.authorizationCodeServices(authorizationCodeServices());//配置授权码模式数据来源 endpoints.tokenStore(tokenStore());//配置令牌存储为数据库存储 // 配置TokenServices参数 DefaultTokenServices tokenServices = new DefaultTokenServices();//修改默认令牌生成服务 tokenServices.setTokenStore(endpoints.getTokenStore());//基于数据库令牌生成 tokenServices.setSupportRefreshToken(true);//是否支持刷新令牌 tokenServices.setReuseRefreshToken(true);//是否重复使用刷新令牌(直到过期) tokenServices.setClientDetailsService(endpoints.getClientDetailsService());//设置客户端信息 tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());//用来控制令牌存储增强策略 //访问令牌的默认有效期(以秒为单位)。过期的令牌为零或负数。 // tokenServices.setAccessTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(30)); // 30天 //刷新令牌的有效性(以秒为单位)。如果小于或等于零,则令牌将不会过期 // tokenServices.setRefreshTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(3)); //3天 endpoints.tokenServices(tokenServices);//使用配置令牌服务 } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.withClientDetails(clientDetails());//使用 jdbc存储 } /** * 用来配置令牌端点的安全约束,也就是这个端点谁能访问,谁不能访问。 */ @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security .allowFormAuthenticationForClients() .tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") ; } /** * 授权码模式数据来源 * @return */ @Bean public AuthorizationCodeServices authorizationCodeServices(){ return new JdbcAuthorizationCodeServices(dataSource); } } package com.zjl.oauth.secuity.config; import lombok.AllArgsConstructor; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; import org.springframework.security.oauth2.provider.token.TokenStore; /** * 资源服务器配置 * @author zjl * @date 2023-03-20 */ @Configuration @AllArgsConstructor @EnableResourceServer @EnableGlobalMethodSecurity(prePostEnabled = true) public class ResourceServerConfig extends ResourceServerConfigurerAdapter { private TokenStore tokenStore; /** * 配置资源服务器安全策略 * @param resources * @throws Exception */ @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.tokenStore(tokenStore); } /** * 配置资源服务器请求授权策略 * @param http * @throws Exception */ @Override public void configure(HttpSecurity http) throws Exception { http .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .requestMatchers() // 被保护的资源,如果不配置,则可以直接访问 .antMatchers("/api/**","/websocket") .and() .authorizeRequests() .antMatchers("/sso/**").permitAll() .antMatchers("/oauth/login").permitAll() .antMatchers("/oauth/check_token").permitAll() // .anyRequest().authenticated() ; } } package com.zjl.oauth.secuity.config; import com.zjl.oauth.secuity.service.MyUserDetailsService; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy; @AllArgsConstructor @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyUserDetailsService userDetailsService; /** * 配置认证信息 * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override @Bean protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } /** * 配置授权信息 * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .and() .authorizeRequests() .antMatchers("/oauth/authorize").authenticated() .anyRequest().permitAll() .and().headers().frameOptions().disable() .and().csrf().disable() ; } }这些是我的配置,里面有导致login打不开的原因吗 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> <version>2.2.5.RELEASE</version> </dependency>这个是我的maven依赖
08-29
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

n_rts

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值