auth2 如果配置gateway进行访问 代码
时间: 2024-02-01 16:04:02 浏览: 30
以下是使用 Spring Cloud Gateway 和 Spring Security OAuth2 进行 OAuth2 认证和授权的示例代码:
1. 配置 application.yml
```yaml
spring:
security:
oauth2:
client:
registration:
my-client:
client-id: my-client-id
client-secret: my-client-secret
scope: read,write
authorization-grant-type: authorization_code
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
client-name: My Client
provider:
my-oauth2:
authorization-uri: https://oauth2.example.com/oauth2/authorize
token-uri: https://oauth2.example.com/oauth2/token
user-info-uri: https://oauth2.example.com/oauth2/userinfo
user-name-attribute: sub
```
2. 配置 GatewaySecurityConfig.java
```java
@Configuration
@EnableWebFluxSecurity
public class GatewaySecurityConfig {
@Autowired
private ReactiveClientRegistrationRepository clientRegistrationRepository;
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers("/login/**", "/webjars/**").permitAll()
.anyExchange().authenticated()
.and()
.oauth2Login()
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/"))
.and()
.csrf().disable();
return http.build();
}
@Bean
public SecurityWebFilterChain springSecurityOAuth2FilterChain(ServerHttpSecurity http) {
// Use the configured OAuth2 client registration repository
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepository,
new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
oauth.setDefaultOAuth2AuthorizedClient(true);
http
.authorizeExchange()
.pathMatchers("/login/**", "/webjars/**").permitAll()
.anyExchange().authenticated()
.and()
.oauth2Login()
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/"))
.and()
.oauth2Client();
return http.build();
}
}
```
3. 配置 RouteConfig.java
```java
@Configuration
public class RouteConfig {
@Autowired
private ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2Client;
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/api/**")
.filters(f -> f.filter(oauth2Client).stripPrefix(1))
.uri("http://localhost:8081"))
.build();
}
}
```
这是一个简单的示例,您需要根据您的具体情况进行调整和扩展。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)