我在使用OAuth2授权用户的带有Spring Security 5应用程序的Spring Boot 2遇到问题。不允许用户查看安全页面,而是将其重定向回授权屏幕。
如果我将OAuth2提供程序切换到Google,则可以正确看到“ / securedPage”
如何摆脱重定向循环并能够看到“ / securedPage”?
我以一种登录页面的方式配置了该页面,用户单击“登录”按钮,然后将其重定向到OAuth2服务器的登录页面。用户能够输入凭据,并被重定向到授权页面,在该页面中,用户可以接受所需的范围。然后,浏览器被重定向到OAuth2客户端Web服务器上的“ / securedPage”,但是请求被重定向回OAuth2授权服务器。
这是我客户的代码。有关客户端和服务器的完整源代码,请访问:https://bitbucket.org/forestg83/spring-boot-oauth/src/master/
Starter.java
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
OAuth2LoginConfig.java
@Configuration
@EnableOAuth2Sso
public class OAuth2LoginConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/index**")
.permitAll()
.anyRequest() // other requests must be authenticated
.authenticated();
}
}
application.yml
server:
port: 80
servlet:
context-path: /ui
session:
cookie:
name: UISESSION
security:
basic:
enabled: false
oauth2:
client:
clientId: first-client
clientSecret: secret
accessTokenUri: http://localhost:8080/oauth/token
userAuthorizationUri: http://localhost:8080/oauth/authorize
preEstablishedRedirectUri: http://localhost/ui/securedPage
useCurrentUri: false
grantType: authorization_code
scope:
- read
tokenName: oauth_token
authenticationScheme: query
resource:
preferTokenInfo: false
userInfoUri: http://localhost:8080/user/me
# clientId: PASTE_YOUR_GOOGLE_CLIENT_ID
# clientSecret: PASTE_YOUR_CLIENT_SECRET
# accessTokenUri: https://oauth2.googleapis.com/token
# userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth
# grantType: authorization_code
# scope:
# - https://www.googleapis.com/auth/calendar
# - profile
# - email
# tokenName: oauth_token
# authenticationScheme: query
# clientAuthenticationScheme: form
# resource:
# preferTokenInfo: false
# userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo
以下是重定向循环的示例:Redirect loop between authorize and securedPage 重定向循环的第二部分:redirect from /securedPage