![](/img/trans.png)
[英]okta oauth2 Spring security all protected pages redirect to login
[英]Spring 5 Security OAuth2 Login Redirect Loop
我想使用Spotify Web API,但是在使用Spring Security Configuration時遇到了麻煩。 這是我的安全性依賴項:
/* springBootVersion = '2.1.2.RELEASE' */
implementation "org.springframework.security:spring-security-oauth2-client"
implementation 'org.springframework.security:spring-security-oauth2-jose:5.1.6.RELEASE'
implementation "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.7.RELEASE"
這是我的application.yml
文件中的安全性:
spring:
security:
oauth2:
client:
registration:
spotify:
provider: spotify-provider
client-id: <client-id>
client-secret: <client-secret>
client-authentication-method: basic
authorization-grant-type: authorization_code
redirect-uri: http://localhost:8080/
scope: <comma delimited scopes>
provider:
spotify-provider:
authorization-uri: https://accounts.spotify.com/authorize
token-uri: https://accounts.spotify.com/api/token
user-info-uri: https://api.spotify.com/v1/me
我的問題是,登錄並重定向回我的應用程序后,它卡在URL http://localhost:8080/oauth2/authorization/spotify
並顯示錯誤
本地主機將您重定向了太多次。
這是我的Java安全性配置:
@Configuration
public class SpotifySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}
嘗試添加@ EnableOAuth2Sso批注:
@Configuration
@EnableOAuth2Sso
public class SpotifySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}
重定向循環是因為/oauth2/authorization/
端點是安全的,因此觸發了返回Web API以獲得訪問令牌的操作。
我已經將配置更新為:
@Configuration
public class SpotifySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/home", "/login**","/callback/", "/webjars/**", "/error**", "/oauth2/authorization/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}
第二個問題是redirect-uri
是Web API將訪問令牌發送到Spring用來獲取刷新令牌的URI。 我以為是成功登錄。 Spring已經有一個處理刷新令牌的實現,但是我不知道它應該使用哪個端點。 由於某些原因,redirect-uri不能為空,沒有默認值,我會收到此錯誤:
IllegalArgumentException:redirectUriTemplate不能為空
要使用Spring的刷新令牌實現,我需要將redirect-uri設置為此:
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
redirect-uri-template
是一個別名redirect-uri
(它們是相同的變量)。
我在另一個stackoverflow帖子中找到了redirect-uri
:
在Spring Security 5 OAuth Client和Spring Boot 2.0中authorizationGrantType不能為null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.