SpringBoot整合Security利用oauth2完成第三方验证登录

导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

 利用gitee来实现第三方登录

1.在gitee个人设置中点击到第三方应用

2.创建自己的应用

        应用主页为登录后跳转的url

        回调地址为第三方通过oauth2,oauth2自动默认设置的地址

3.创建后

        id和secret讲会配置在application配置文件中

        登陆后浏览器会存在cookie,测试需要快速经常访问,可点击移除按钮,消除令牌,方便测试登录

 配置application.properties,配置第三方登录需要的配置,springboot服务端口默认端口为:8080,可配可不配,

# client-id 查看gitee颁发的
spring.security.oauth2.client.registration.gitee.client-id=d449c6d3a667836d7070cbf2c11f0642f9fe0c13ed332e031425ff80e3cd65d2
# client-secret 查看gitee颁发的
spring.security.oauth2.client.registration.gitee.client-secret=edea20934c7fced2add76b146cd154f1dd55a800421c73bc238f82dffd860423
# 认证方式
spring.security.oauth2.client.registration.gitee.authorization-grant-type=authorization_code
# redirect-uri:回调地址,填写的与Gitee上申请的一致
spring.security.oauth2.client.registration.gitee.redirect-uri=http://localhost:8080/login/oauth2/code/gitee
# 接下来这个不是必须的 客户端名称,可以在登录选择页面上显示
spring.security.oauth2.client.registration.gitee.client-name=gitee-login

# authorization-uri:授权服务器地址
spring.security.oauth2.client.provider.gitee.authorization-uri=https://gitee.com/oauth/authorize
# token-uri:授权服务器获取token地址
spring.security.oauth2.client.provider.gitee.token-uri=https://gitee.com/oauth/token
# user-info-uri:授权服务器获取用户信息的地址
spring.security.oauth2.client.provider.gitee.user-info-uri=https://gitee.com/api/v5/user
# user-name-attribute:用户信息中的用户名属性 可以自定义
spring.security.oauth2.client.provider.gitee.user-name-attribute=name

 偏写配置类

@Configuration
public class SecurityConfig  {

    @Bean
    protected SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception {
        //开启登录拦截
        httpSecurity.authorizeHttpRequests().anyRequest().authenticated();
        //开启oauth2第三方登录
        httpSecurity.oauth2Login();
        //开启登录
        httpSecurity.formLogin();
        return httpSecurity.build();
    }
}

编写controller,测试用例

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/user2")
    public DefaultOAuth2User hello(){
        //获取Security请求头中的信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        //返回到浏览器查看信息
        return (DefaultOAuth2User)authentication.getPrincipal();
    }
}

 开启springboot

 访问服务的Security默认登录地址:http://localhost:8080/login,点击下面小的gitee登录

 测试登录

登录后访问自己定义的controller, 

 

 ​​​​​​​

 

 登录访问出问题或者二次登录不显示以上页面,二次登录需清除Token,清除后需要等待一段时间才可访问。另外令牌Token存在即使启动在多次springboot也不会出现以上页面,有登录缓存,不需要在二次登录。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Spring Boot 整合 Spring Security OAuth2 的代码示例: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.3.RELEASE</version> </dependency> ``` 2. 配置 OAuth2 客户端 在 application.yml 文件中添加以下配置: ``` security: oauth2: client: clientId: your-client-id clientSecret: your-client-secret accessTokenUri: https://your-auth-server.com/oauth/token userAuthorizationUri: https://your-auth-server.com/oauth/authorize clientAuthenticationScheme: form scope: read write ``` 3. 配置 Spring Security 创建一个继承自 WebSecurityConfigurerAdapter 的类,并添加以下配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/oauth/**").permitAll() .anyRequest().authenticated() .and() .formLogin().permitAll() .and() .csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 4. 创建资源服务器 创建一个继承自 ResourceServerConfigurerAdapter 的类,并添加以下配置: ``` @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("resource-id"); } } ``` 5. 创建控制器 创建一个简单的控制器,用于测试 OAuth2 认证: ``` @RestController public class TestController { @GetMapping("/api/test") public String test() { return "Hello, World!"; } } ``` 6. 运行应用程序 现在你可以运行应用程序,并访问 http://localhost:8080/api/test 进行测试。如果你没有提供正确的 OAuth2 认证信息,你将会收到一个 401 Unauthorized 错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值