如何使用春季启动安全添加谷歌登录按钮



我需要添加一个谷歌登录按钮。我有如下配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(a -> a
.antMatchers("/", "/error", "/test", "/webjars/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(fl -> fl.loginPage("/test.html"))
.exceptionHandling(e -> e
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
)
.csrf(c -> c
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
)
.logout(l -> l
.logoutSuccessUrl("/").permitAll()
)
.oauth2Login();
}
}

我有带有谷歌按钮的index.html页面,但这个按钮看起来不像谷歌登录按钮:

<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SignIn</title>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css"/>
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<header th:insert="fragments/header.html :: emptyNav"></header>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
</body>
</html>

我想使用spring-security进行社交登录,并获得谷歌风格的谷歌登录按钮
我该怎么做?

Google sign-in使用OAuth2身份验证。

  1. 您需要在谷歌中注册您的应用程序(https://console.developers.google.com/apis/credentials)
  2. 获取您的CLIENT_CODE和CLIENT_SECRET以访问您的应用程序
  3. 在你的应用程序中实现"密码"OAuth2工作流(例如,按钮调用控制器的方法,它调用服务,你用Authorization: Basic Base64(CLIENT_CODE:CLIENT_SECRET)向谷歌提出请求,并用用户登录/密码表单(

最新更新