大佬教程收集整理的这篇文章主要介绍了是否可以为 Spring Boot 应用程序提供 oauth 和基本用户名密码登录选项?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试通过提供基于用户名和密码的登录以及用于用户身份验证的 Google oAuth 来实现带有 Spring Boot 的基本登录页面。
想知道 spring 是否允许使用相同的应用程序完成此操作。
任何帮助将不胜感激。
是的,可以做到。您需要同时配置 OAuth2 和 Form Login 您的 WebSecurityConfigurer 并配置自定义登录页面。您还需要配置一个 passwordEncoder 和 UserDetailsservice 用于表单登录和 OAuth2Userservice (可能还有一个 OidcUserservicE) 用于 OAuth2 登录。这 Principal 实现将与登录类型相对应。
WebSecurityConfigurer.configure(@R_696_10107@Security)
:
@Override
protected void configure(@R_696_10107@Security @R_696_10107@) throws Exception {
@R_696_10107@.antMatcher("/**")
.authorizerequests(t -> t.anyrequest().authenticated())
.formLogin(t -> t.loginPage("/login").permitAll())
.oauth2Login(Customizer.withDefaults())
.logout(t -> t.logoutrequestMatcher(new AntPathrequestMatcher("/logout"))
.logoutsuccessUrl("/").permitAll());
...
}
自定义登录页面,包括表单登录和 OAuth2 提供程序链接:
<div>
<div>
<div>
<form th:object="${form}">
<input type="email" th:name="username" th:placeholder="'E\'mail Address'"/>
<label th:text="'E\'mail Address'"/>
<input type="password" th:name="password" th:placeholder="'password'"/>
<label th:text="'password'"/>
<button type="submit" th:text="'Login'"/>
<th:block th:if="${! oauth2.isEmpty()}">
<hr/>
<a th:each="client : ${oauth2}" th:href="@{/oauth2/authorization/{iD}(id=${Client.registrationID})}" th:text="${Client.clientNamE}"/>
</th:block>
</form>
</div>
</div>
<div>
<div>
<p th:if="${param.error}">Invalid username and password.</p>
<p th:if="${param.logout}">You have been logged out.</p>
</div>
</div>
</div>
OAuth2 配置:
---
spring:
security:
oauth2:
client:
registration:
google:
client-id: XXXXXXXXXXXXXXXXXXXX
client-secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
provider:
google:
user-name-attribute: email
这来自我的文章 @R_696_10107@s://blog.hcf.dev/article/2020-10-31-spring-boot-part-07(源代码 在 @R_696_10107@s://github.com/ALLEN-ball/spring-boot-web-server/tree/trunk/part-07).
以上是大佬教程为你收集整理的是否可以为 Spring Boot 应用程序提供 oauth 和基本用户名密码登录选项?全部内容,希望文章能够帮你解决是否可以为 Spring Boot 应用程序提供 oauth 和基本用户名密码登录选项?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。