Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
liuhui
/
sinobase-nacos-gateway
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7570672a
authored
Apr 25, 2021
by
liuhui
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Initial commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
278 additions
and
0 deletions
.gitignore
pom.xml
src/main/java/com/sinobase/nacos/AuthFilter.java
src/main/java/com/sinobase/nacos/GatewayRegistration.java
src/main/resources/application.yml
.gitignore
0 → 100644
View file @
7570672a
.DS_Store
######################################################################
# Build Tools
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
target/
!.mvn/wrapper/maven-wrapper.jar
######################################################################
# IDE
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
*/.externalToolBuilders/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/*
nbbuild/
dist/
nbdist/
.nb-gradle/
######################################################################
# Others
*.log
*.xml.versionsBackup
!*/build/*.java
!*/build/*.html
!*/build/*.xml
pom.xml
0 → 100644
View file @
7570672a
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.sinobase
</groupId>
<artifactId>
sinobase-eureka-service
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<name>
sinobase-eureka-service
</name>
<parent>
<groupId>
com.sinobase
</groupId>
<artifactId>
sinobase
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
</parent>
<properties>
<java.version>
1.8
</java.version>
<spring-cloud.version>
Greenwich.SR2
</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-gateway
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-webflux
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-dependencies
</artifactId>
<version>
${spring-cloud.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>
${project.artifactId}
</finalName>
<resources>
<resource>
<directory>
src/main/resources
</directory>
<filtering>
true
</filtering>
<includes>
<include>
application.yml
</include>
<include>
application-${profileActive}.yml
</include>
<include>
banner.txt
</include>
<include>
mybatis/**/*
</include>
<include>
static/**/*
</include>
<include>
templates/**/*
</include>
<include>
ehcache/*
</include>
<include>
log4j.xml
</include>
<include>
log/*
</include>
</includes>
<excludes>
<exclude>
**/*.woff
</exclude>
<exclude>
**/*.woff2
</exclude>
<exclude>
**/*.ttf
</exclude>
<exclude>
**/*.doc
</exclude>
<exclude>
**/*.docx
</exclude>
</excludes>
</resource>
<resource>
<directory>
src/main/resources
</directory>
<filtering>
false
</filtering>
<includes>
<include>
**/*.woff
</include>
<include>
**/*.woff2
</include>
<include>
**/*.ttf
</include>
<include>
**/*.doc
</include>
<include>
**/*.docx
</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-war-plugin
</artifactId>
<configuration>
<failOnMissingWebXml>
false
</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
<configuration>
<includeSystemScope>
true
</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>
repackage
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.1
</version>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
src/main/java/com/sinobase/nacos/AuthFilter.java
0 → 100644
View file @
7570672a
package
com
.
sinobase
.
nacos
;
import
java.nio.charset.StandardCharsets
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
import
com.alibaba.fastjson.JSONObject
;
import
reactor.core.publisher.Mono
;
@Component
public
class
AuthFilter
implements
GlobalFilter
,
Ordered
{
@Autowired
private
RedisTemplate
<
Object
,
Object
>
redisTemplate
;
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
//String token = exchange.getRequest().getQueryParams().getFirst("authToken");
String
user
=
(
String
)
redisTemplate
.
opsForValue
().
get
(
"165038"
);
JSONObject
json
=
JSONObject
.
parseObject
(
user
);
String
token
=
(
String
)
json
.
get
(
"userId"
);
//返回401状态码和提示信息
if
(
StringUtils
.
isBlank
(
token
))
{
ServerHttpResponse
response
=
exchange
.
getResponse
();
JSONObject
message
=
new
JSONObject
();
message
.
put
(
"status"
,
-
1
);
message
.
put
(
"data"
,
"鉴权失败"
);
byte
[]
bits
=
message
.
toJSONString
().
getBytes
(
StandardCharsets
.
UTF_8
);
DataBuffer
buffer
=
response
.
bufferFactory
().
wrap
(
bits
);
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
//指定编码,否则在浏览器中会中文乱码
response
.
getHeaders
().
add
(
"Content-Type"
,
"text/plain;charset=UTF-8"
);
return
response
.
writeWith
(
Mono
.
just
(
buffer
));
}
return
chain
.
filter
(
exchange
);
}
@Override
public
int
getOrder
()
{
return
-
100
;
}
}
\ No newline at end of file
src/main/java/com/sinobase/nacos/GatewayRegistration.java
0 → 100644
View file @
7570672a
package
com
.
sinobase
.
nacos
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
@SpringBootApplication
@EnableDiscoveryClient
public
class
GatewayRegistration
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
GatewayRegistration
.
class
,
args
);
}
}
src/main/resources/application.yml
0 → 100644
View file @
7570672a
server
:
port
:
9000
spring
:
application
:
name
:
gateway
cloud
:
gateway
:
discovery
:
locator
:
enabled
:
true
nacos
:
discovery
:
server-addr
:
http://172.30.13.54:8848/
redis
:
host
:
172.30.13.53
port
:
6379
password
:
"
"
#连接超时时间(毫秒)
timeout
:
2000
pool
:
#最大连接数(负数表示没有限制)
max-active
:
1000
#最大空闲连接
max-idle
:
10
#最大阻塞等待时间(负数表示没有限制)
max-wait
:
100000
database
:
0
management
:
endpoints
:
web
:
exposure
:
include
:
"
*"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment