Auth2 授权登陆

原来的格局

原来斗罗大陆有各种租赁狗子的公司,每个公司有很多门店,门店都只放自己家的狗子,每个会员在对应公司的门店注入他们注册会员时账户密码就可以领取狗子耍。

后来

后来这片大陆,出现了越来越多的公司,用户在多家公司开了会员,租赁狗子需要不同的门店输入对应的密码。
这片大陆出现了一个Boss,他下令一个地方,只允许开一个租赁市场,所有不同公司的狗子都放在这里一个市场里面。市场给每个公司一个clientId和clientSecret。他们在这个市场摆摊位。

一天,该市场的一个会员,来到这片市场闲逛,停在一个叫陌陌租赁的摊贩面前,他告诉这个摊主要租赁他们的狗子。摊主就和市场的老板电话,说我是你市场的摊主,在你市场租了一个摊位,这是你给我的clientId和clientSecret。老板对了下clientId和clientSecret 发现没问题。然后call 这个租赁狗子的人,你要租赁的摊主是我市场的没问题呀,现在人家要你账户信息给他,你同意吗。
用户说我同意呀(类似于确认授权),给老板。市场老板收到用户的确认,给了摊主一个授权码,你拿着这个授权码找我的助手去获取用户信息呀。摊主拿着这个授权码和助手的联系方式去获得用户的信息。
助手检查这个授权码没问题。给了摊主一把市场的会员信息的钥匙钥匙会过期,又给了一个的凭证。
accessToken和refreshToken;。摊主拿到这个就可以想市场管理者一样访问用户的信息啦。
摊主拿着这个去获得用户信息。对比该用户用户是否已经注册过

auth2 授权登陆

https://www.jianshu.com/p/2afc34815b90

授权码流程

在这里插入图片描述
在这里插入图片描述

参考文档

https://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html

### OAuth2 登录实现教程 OAuth2 是一种授权框架,主要用于允许第三方应用程序安全地获取用户资源而不暴露其凭据。以下是基于 Spring Boot 和 OAuth2 的登录功能实现教程。 #### 1. 配置 `application.yml` 文件 在项目中配置 OAuth2 客户端和服务端的相关参数: ```yaml server: port: 8082 servlet: context-path: /memberSystem security: oauth2: client: client-id: UserManagement client-secret: user123 access-token-uri: http://localhost:8080/oauth/token user-authorization-uri: http://localhost:8080/oauth/authorize resource: jwt: key-uri: http://localhost:8080/oauth/token_key ``` 此部分定义了客户端 ID、密钥以及访问令牌 URI 等必要信息[^1]。 #### 2. 添加依赖项 确保项目的 `pom.xml` 中包含必要的 Maven 依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-jose</artifactId> </dependency> ``` 这些依赖项用于支持 OAuth2 客户端集成和 JWT 处理[^3]。 #### 3. 创建 Security 配置类 通过自定义的安全配置启用 OAuth2 支持: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter; import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator; import org.springframework.security.oauth2.jwt.JwtDecoder; import org.springframework.security.oauth2.jwt.NimbusJwtEncoder; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private final ClientRegistrationRepository clientRegistrationRepository; public SecurityConfig(ClientRegistrationRepository clientRegistrationRepository) { this.clientRegistrationRepository = clientRegistrationRepository; } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/oauth2/**").permitAll() // 允许未认证用户访问登录页面和 OAuth2 接口 .anyRequest().authenticated(); // 所有其他请求都需要身份验证 http.oauth2Login(oauth2 -> oauth2 .clientRegistrationRepository(clientRegistrationRepository)); JwtDecoder decoder = new NimbusJwtDecoder(JwksSupport.load(jwkSetUri)); // 加载 JWK 密钥集 DelegatingOAuth2TokenValidator validator = ...; // 自定义 Token Validator JwtAuthenticationConverter converter = customJwtAuthenticationConverter(); } private Converter<Jwt, ? extends AbstractAuthenticationToken> customJwtAuthenticationConverter() { JwtAuthenticationConverter converter = new JwtAuthenticationConverter(); converter.setJwtGrantedAuthoritiesConverter(...); // 转换 Authorities return converter; } } ``` 这段代码实现了 OAuth2 登录流程,并设置了 JWT 解码器和权限转换逻辑。 #### 4. 测试 OAuth2 登录 启动服务后,在浏览器中导航到 `/memberSystem/login` 页面,系统会重定向至 OAuth2 提供商的授权界面。完成授权后,返回的应用程序将携带有效的 Access Token[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值