Java17 --- SpringSecurity之OAuth2

Java17 --- SpringSecurity之OAuth2

码农世界 2024-06-21 后端 96 次浏览 0个评论

一、OAuth2

1.1、使用github以授权码方式

1.1.1、注册应用程序

Java17 --- SpringSecurity之OAuth2 

Java17 --- SpringSecurity之OAuth2 

1.1.2、测试代码

 pom依赖


            org.springframework.boot
            spring-boot-starter-oauth2-client
        
spring:
  security:
    oauth2:
      client:
        registration:
          github:
            client-id: Ov23liQnQPka0x1qX6ZI
            client-secret: 278f3ad4ecdd8dad4b318d7bd8d33f2bb9428019
@Controller
public class IndexController {
    @GetMapping("/")
    public String index(Model model, @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                        @AuthenticationPrincipal OAuth2User oauth2User) {
        model.addAttribute("userName", oauth2User.getName());
        model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
        model.addAttribute("userAttributes", oauth2User.getAttributes());
        return "index";
    }
}



  Spring Security - OAuth 2.0 Login
  


  
    User: 
  
   
  
    

OAuth 2.0 Login with Spring Security

You are successfully logged in via the OAuth 2.0 Client   User Attributes:
  • :

 

 

 

转载请注明来自码农世界,本文标题:《Java17 --- SpringSecurity之OAuth2》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,96人围观)参与讨论

还没有评论,来说两句吧...

Top