使用Nginx和Spring Gateway为SkyWalking的增加登录认证功能

这篇具有很好参考价值的文章主要介绍了使用Nginx和Spring Gateway为SkyWalking的增加登录认证功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


SkyWalking的可视化后台是没有用户认证功能的,默认下所有知道地址的用户都能访问,官网是建议通过网关增加认证。
本文介绍通过Nginx和Spring Gateway两种方式

1、使用Nginx增加认证。

生成密钥

yum install -y httpd-tools
htpasswd -cb nginx/htpasswd skywalking rtgdbhyffddu#

配置nginx

worker_processes 1;
error_log stderr notice;
events {
    worker_connections 1024;
}
http {
    variables_hash_max_size 1024;
    access_log off;
    #ireal_ip_header X-Real-IP;
    charset utf-8;
    server {
        listen 8081;
        #auth_basic "Please enter the user name and password"; #这里是验证时的提示信息
        #auth_basic_user_file /data/skywalking/nginx/htpasswd;
        index  index.html;
        location / {
            root   html;
			index  index.html index.htm;
            #auth_basic on;
			auth_basic "Please enter the user name and password"; #这里是验证时的提示信息
            auth_basic_user_file /etc/nginx/htpasswd;
            proxy_pass http://172.17.0.9:8080;
            # WebSocket 穿透
            #proxy_set_header Origin "";
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "upgrade";
        }
    }
}

密码配置:/etc/nginx/htpasswd

skywalking:$apr1$FVaUB8RE$.brXLk5N.IsNRqm3.Vy2n1

skywalking 登录,java,nginx,spring,gateway
skywalking 登录,java,nginx,spring,gateway

2、使用Spring Gateway增加认证

主要是使用Spring Gateway和Spring Security的基础认证formLogin实现,
pom.xml使用的依赖包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.penngo.web.gateway</groupId>
    <artifactId>web_gateway</artifactId>
    <version>1.0.0</version>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2022.0.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.penngo.web.gateway.WebGatewayMain</mainClass>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

WebGatewayMain.java

package com.penngo.web.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebGatewayMain {
    public static void main(String[] args) {
        SpringApplication.run(WebGatewayMain.class, args);
    }
}

SecurityConfiguration.java配置

package com.penngo.web.gateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.server.SecurityWebFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@EnableWebFluxSecurity
@Configuration
public class SecurityConfiguration {

    @Bean
    SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
        http
                .authorizeExchange((authorize) -> authorize
                        .anyExchange().authenticated()
                )
                .cors(cors->cors.disable())
                .csrf(csrf->csrf.disable())
                .formLogin(withDefaults());

        return http.build();
    }

    /**
     * 可以在代码中配置密码,也可以在application.xml中配置密码
     * @return
     */
//    @Bean
//    MapReactiveUserDetailsService userDetailsService() {
//
//        UserDetails user = User.withDefaultPasswordEncoder()
//                .username("admin")
//                .password("123456")
//                .roles("USER")
//                .build();
//        return new MapReactiveUserDetailsService(user);
//    }
}

application.yml

server:
  port: 8081
  servlet:
    encoding:
      force: true
      charset: UTF-8
      enabled: true
spring:
  application:
    name: gatewayapp
  security:
    user:
      name: admin2
      password: 123456

bootstrap.yml

spring:
  cloud:
    gateway:
      routes:
        - id: skywalking
          uri: http://localhost:8080/
          # 绑定ip白名单
          predicates:
            - RemoteAddr=127.0.0.1/32,192.168.245.65/32

运行效果
skywalking 登录,java,nginx,spring,gateway
skywalking 登录,java,nginx,spring,gateway文章来源地址https://www.toymoban.com/news/detail-837161.html

到了这里,关于使用Nginx和Spring Gateway为SkyWalking的增加登录认证功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 在Spring boot中 使用JWT和过滤器实现登录认证

    在Spring boot中 使用JWT和过滤器实现登录认证

    在navicat中运行如下sql,准备一张user表 导入pom.xml坐标 在工utils包下创建一个用于生成和解析JWT 令牌的工具类 在pojo包下创建user类 在mapper包下添加 UserMapper接口 在service包下添加 UserService类 在utils包下添加统一响应结果封装类 在controller包下添加LoginController类 这样登录获取toke

    2024年02月06日
    浏览(12)
  • 使用Spring Boot Security 实现多认证 手机号登录 微信扫码登录 微信扫码注册

    使用Spring Boot Security 实现多认证 手机号登录 微信扫码登录 微信扫码注册

    Spring Boot 3.x Spring Security 5.7 Spring Redis MyBatis plus 前端 Vue 公司 最近有个新项目 使用单点登录 sso 百度了一圈 也没怎么找到微信扫码注册的功能于是自己写 需求就是 手机 + 密码登录 微信扫码登录 微信扫码注册 微信二维码 登录 和注册二合一 具体实现 稍后我会说 本教程将指导

    2024年04月10日
    浏览(15)
  • Spring Authorization Server入门 (十六) Spring Cloud Gateway对接认证服务

    Spring Authorization Server入门 (十六) Spring Cloud Gateway对接认证服务

            之前虽然单独讲过Security Client和Resource Server的对接,但是都是基于Spring webmvc的,Gateway这种非阻塞式的网关是基于webflux的,对于集成Security相关内容略有不同,且涉及到代理其它微服务,所以会稍微比较麻烦些,今天就带大家来实现Gateway网关对接OAuth2认证服务。

    2024年02月10日
    浏览(12)
  • Kafka增加安全验证安全认证,SASL认证,并通过spring boot-Java客户端连接配置

    公司Kafka一直没做安全验证,由于是诱捕程序故需要面向外网连接,需要增加Kafka连接验证,保证Kafka不被非法连接,故开始研究Kafka安全验证 使用Kafka版本为2.4.0版本,主要参考官方文档 官网对2.4版本安全验证介绍以及使用方式地址: https://kafka.apache.org/24/documentation.html#secu

    2024年02月01日
    浏览(46)
  • Spring Gateway、Sa-Token、nacos完成认证/鉴权

    Spring Gateway、Sa-Token、nacos完成认证/鉴权

    之前进行鉴权、授权都要写一大堆代码。如果使用像Spring Security这样的框架,又要花好多时间学习,拿过来一用,好多配置项也不知道是干嘛用的,又不想了解。要是不用Spring Security,token的生成、校验、刷新,权限的验证分配,又全要自己写,想想都头大。 Spring Security太重

    2024年02月09日
    浏览(15)
  • Spring Cloud Gateway + Oauth2 实现统一认证和鉴权!

    Spring Cloud Gateway + Oauth2 实现统一认证和鉴权!

    micro-oauth2-gateway:网关服务,负责请求转发和鉴权功能,整合Spring Security+Oauth2; micro-oauth2-auth:Oauth2认证服务,负责对登录用户进行认证,整合Spring Security+Oauth2; micro-oauth2-api:受保护的API服务,用户鉴权通过后可以访问该服务,不整合Spring Security+Oauth2。 我们首先来搭建认

    2024年01月16日
    浏览(14)
  • Spring Cloud Gateway 整合OAuth2.0 实现统一认证授权

    Spring Cloud Gateway 整合OAuth2.0 实现统一认证授权 GateWay——向其他服务传递参数数据 https://blog.csdn.net/qq_38322527/article/details/126530849 @EnableAuthorizationServer Oauth2ServerConfig 验证签名 网关服务需要RSA的公钥来验证签名是否合法,所以认证服务需要有个接口把公钥暴露出来 接下来搭建网

    2024年02月13日
    浏览(12)
  • 『Nginx安全访问控制』利用Nginx实现账号密码认证登录的最佳实践

    『Nginx安全访问控制』利用Nginx实现账号密码认证登录的最佳实践

    📣读完这篇文章里你能收获到 如何创建用户账号和密码文件,并生成加密密码 配置Nginx的认证模块,实现基于账号密码的登录验证 在Web应用程序的开发中,安全性是一项至关重要的任务。当用户需要访问敏感信息或执行特定操作时,需要使用账号和密码进行身份验证。本文

    2024年02月03日
    浏览(14)
  • Spring Gateway+Security+OAuth2+RBAC 实现SSO统一认证平台

    Spring Gateway+Security+OAuth2+RBAC 实现SSO统一认证平台

    背景:新项目准备用SSO来整合之前多个项目的登录和权限,同时引入网关来做后续的服务限流之类的操作,所以搭建了下面这个系统雏形。 : Spring Gateway, Spring Security, JWT, OAuth2, Nacos, Redis, Danymic datasource, Javax, thymeleaf 如果对上面这些技术感兴趣,可以继续往下阅读 如

    2024年02月13日
    浏览(15)
  • Spring Security进行登录认证和授权

    Spring Security进行登录认证和授权

    用户首次登录提交用户名和密码后spring security 的 UsernamePasswordAuthenticationFilter 把用户名密码封装 Authentication 对象 然后内部调用 ProvideManager 的 authenticate 方法进行认证,然后 ProvideManager 进一步通过内部调用 DaoAuthencationPriovider 的 authenticate 方法进行认证 DaoAuthencationPriovider 通过

    2024年02月11日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包