如何在使用 OAuth2 社交登录保护 REST API 的同时配置 Spring 启动登录页面
How to configure Spring boot login page while securing REST API using OAuth2 social login
我有一个使用 Spring OAuth2 身份验证的示例 Web 应用程序 Google。 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")
;
}
现在,每当发出未经身份验证的请求时,此配置都会将浏览器重定向到登录页面。此页面显示 link 到 Google 的身份验证 Login with Google 以及登录表单。
但我希望浏览器直接重定向到 Google 的身份验证服务器,而不是应用程序的登录页面。我不想要任何形式的 login/signup 功能。我只希望用户使用 Google.
登录
需要对上述配置进行哪些更改才能实现?
尝试从您的配置中删除 formLogin
和 loginPage
..
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")
;
}
我有一个使用 Spring OAuth2 身份验证的示例 Web 应用程序 Google。 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")
;
}
现在,每当发出未经身份验证的请求时,此配置都会将浏览器重定向到登录页面。此页面显示 link 到 Google 的身份验证 Login with Google 以及登录表单。
但我希望浏览器直接重定向到 Google 的身份验证服务器,而不是应用程序的登录页面。我不想要任何形式的 login/signup 功能。我只希望用户使用 Google.
登录需要对上述配置进行哪些更改才能实现?
尝试从您的配置中删除 formLogin
和 loginPage
..
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")
;
}