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
|