如何配置Spring引导登录页面,同时使用OAuth2社会登录保护REST API



我有一个使用Google的Spring OAuth2身份验证的示例web应用程序。HttpSecurity配置如下:

protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/login", "/oauth/**", "/oauth2/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.loginPage("/login")
.usernameParameter("email")
.passwordParameter("pass")
.defaultSuccessUrl("/list")
.and()
.oauth2Login()
.loginPage("/login")
.userInfoEndpoint()
.userService(oauthUserService)
.and()
.successHandler( ... NOT SHOWN ...)
.and()
.logout().logoutSuccessUrl("/").permitAll()
.and()
.exceptionHandling().accessDeniedPage("/403")
;
}

现在,无论何时发出未经身份验证的请求,该配置都会将浏览器重定向到登录页面。这个页面显示了一个链接到Google的身份验证Login with Google以及一个登录表单。

但是我希望浏览器直接重定向到Google的身份验证服务器,而不是应用程序的登录页面。我不想要任何形式的登录/注册功能。我只想让用户使用Google登录。

需要在上述配置中进行哪些更改才能实现这一点?

尝试从您的配置中删除formLoginloginPage.

protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/login", "/oauth/**", "/oauth2/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.userInfoEndpoint()
.userService(oauthUserService)
.and()
.successHandler( ... NOT SHOWN ...)
.and()
.logout().logoutSuccessUrl("/").permitAll()
.and()
.exceptionHandling().accessDeniedPage("/403")
;
}

最新更新