spring security webflux github 授权登录

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:58   2933   0

spring security webflux github 授权登录

使用三方用户github授权登陆

***********************

示例

*******************

配置文件

application.yml

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            client-id: ******
            client-secret: ******

*******************

config 层

WebSecurityConfig

@Configuration
public class WebSecurityConfig {

    @Bean
    public SecurityWebFilterChain initSecurityWebFilterChain(ServerHttpSecurity http){
        http.formLogin().loginPage("/login");

        http.oauth2Login().and().authorizeExchange()
                .pathMatchers("/hello2").hasRole("USER")
                .pathMatchers("/**").permitAll();

        return http.build();
    }
}

*******************

controller 层

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello2")
    public String hello(Principal principal){
        return "hello "+principal.getName();
    }

    @RequestMapping("/")
    public String redirect(){
        return "redirect";
    }
}

*******************

前端页面

login.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:align="center">
    <a th:href="@{/oauth2/authorization/github}">github</a>
</div>
</body>
</html>

***********************

使用测试

localhost:8080/hello2

认证通过后,输出:hello 41827785

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP