Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zlt2000
microservices-platform
提交
417b4392
microservices-platform
项目概览
zlt2000
/
microservices-platform
8 个月 前同步成功
通知
16
Star
4
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
417b4392
编写于
6月 26, 2022
作者:
Z
zhult13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加资源服务demo(无网络隔离)
上级
51b69faa
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
217 addition
and
15 deletion
+217
-15
zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java
...va/com/central/common/resolver/TokenArgumentResolver.java
+30
-15
zlt-demo/pom.xml
zlt-demo/pom.xml
+2
-0
zlt-demo/resources-server-demo/README.md
zlt-demo/resources-server-demo/README.md
+36
-0
zlt-demo/resources-server-demo/pom.xml
zlt-demo/resources-server-demo/pom.xml
+51
-0
zlt-demo/resources-server-demo/src/main/java/org/zlt/ResourcesServerApp.java
...server-demo/src/main/java/org/zlt/ResourcesServerApp.java
+18
-0
zlt-demo/resources-server-demo/src/main/java/org/zlt/config/MyResourceConfig.java
...r-demo/src/main/java/org/zlt/config/MyResourceConfig.java
+20
-0
zlt-demo/resources-server-demo/src/main/java/org/zlt/config/WebMvcConfig.java
...erver-demo/src/main/java/org/zlt/config/WebMvcConfig.java
+18
-0
zlt-demo/resources-server-demo/src/main/java/org/zlt/controller/TestController.java
...demo/src/main/java/org/zlt/controller/TestController.java
+28
-0
zlt-demo/resources-server-demo/src/main/resources/bootstrap.yml
...mo/resources-server-demo/src/main/resources/bootstrap.yml
+14
-0
未找到文件。
zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java
浏览文件 @
417b4392
...
...
@@ -8,6 +8,9 @@ import com.central.common.model.SysRole;
import
com.central.common.model.SysUser
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.security.authentication.AnonymousAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.web.bind.support.WebDataBinderFactory
;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
...
...
@@ -66,25 +69,37 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
String
roles
=
request
.
getHeader
(
SecurityConstants
.
ROLE_HEADER
);
//账号类型
String
accountType
=
request
.
getHeader
(
SecurityConstants
.
ACCOUNT_TYPE_HEADER
);
SysUser
user
=
null
;
if
(
StrUtil
.
isBlank
(
username
))
{
log
.
warn
(
"resolveArgument error username is empty"
);
return
null
;
}
SysUser
user
;
if
(
isFull
)
{
user
=
userService
.
selectByUsername
(
username
);
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
if
(
authentication
!=
null
&&
!(
authentication
instanceof
AnonymousAuthenticationToken
))
{
Object
principal
=
authentication
.
getPrincipal
();
//客户端模式只返回一个clientId
if
(
principal
instanceof
SysUser
)
{
user
=
(
SysUser
)
principal
;
}
}
if
(
user
==
null
)
{
return
null
;
}
}
else
{
user
=
new
SysUser
();
user
.
setId
(
Long
.
valueOf
(
userId
));
user
.
setUsername
(
username
);
if
(
isFull
)
{
user
=
userService
.
selectByUsername
(
username
);
}
else
{
user
=
new
SysUser
();
user
.
setId
(
Long
.
valueOf
(
userId
));
user
.
setUsername
(
username
);
}
List
<
SysRole
>
sysRoleList
=
new
ArrayList
<>();
Arrays
.
stream
(
roles
.
split
(
","
)).
forEach
(
role
->
{
SysRole
sysRole
=
new
SysRole
();
sysRole
.
setCode
(
role
);
sysRoleList
.
add
(
sysRole
);
});
user
.
setRoles
(
sysRoleList
);
}
List
<
SysRole
>
sysRoleList
=
new
ArrayList
<>();
Arrays
.
stream
(
roles
.
split
(
","
)).
forEach
(
role
->
{
SysRole
sysRole
=
new
SysRole
();
sysRole
.
setCode
(
role
);
sysRoleList
.
add
(
sysRole
);
});
user
.
setRoles
(
sysRoleList
);
return
user
;
}
}
zlt-demo/pom.xml
浏览文件 @
417b4392
...
...
@@ -24,5 +24,7 @@
<module>
dubbo-demo
</module>
<!-- webSocket集成demo -->
<module>
websocket-demo
</module>
<!-- 资源服务器demo -->
<module>
resources-server-demo
</module>
</modules>
</project>
\ No newline at end of file
zlt-demo/resources-server-demo/README.md
0 → 100644
浏览文件 @
417b4392
## 一、说明
资源服务器 demo 样例,以最简化的代码演示如何快速集成一个带鉴权功能的服务,适用于
`无网络隔离`
架构。
> 关于无网络隔离架构的设计可参考文档:[无网络隔离架构](https://www.kancloud.cn/zlt2000/microservices-platform/1153640)
## 二、启动以下服务
1.
zlt-uaa:统一认证中心
2.
user-center:用户服务
3.
sc-gateway:api网关
4.
resources-server-demo
> 环境配置与启动参考文档:https://www.kancloud.cn/zlt2000/microservices-platform/919418
## 三、测试
### 3.1. 测试接口一
http://localhost:8093/test/notAuth
> 无需token访问
### 3.2. 测试接口二
http://localhost:8093/test/auth?access_token=xxx
> - xxx 需替换为正确的 access_token
> - 可以通过修改 `bootstrap.yml` 文件中的 `zlt.security.ignore.httpUrls` 参数添加排除校验的url。
## 四、获取access_token
可使用任意授权模式获取;
例如:密码模式授权
-
请求方式:POST
-
请求头:Authorization:Basic d2ViQXBwOndlYkFwcA==
-
请求地址:http://localhost:9900/api-uaa/oauth/token?grant_type=password&username=admin&password=admin
> 授权接口清单参考文档:https://www.kancloud.cn/zlt2000/microservices-platform/1158135
zlt-demo/resources-server-demo/pom.xml
0 → 100644
浏览文件 @
417b4392
<?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>
<parent>
<groupId>
com.zlt
</groupId>
<artifactId>
zlt-demo
</artifactId>
<version>
5.3.0
</version>
</parent>
<artifactId>
resources-server-demo
</artifactId>
<dependencies>
<dependency>
<groupId>
com.zlt
</groupId>
<artifactId>
zlt-config
</artifactId>
</dependency>
<dependency>
<groupId>
com.zlt
</groupId>
<artifactId>
zlt-auth-client-spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
com.zlt
</groupId>
<artifactId>
zlt-redis-spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-extension
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
<executions>
<execution>
<goals>
<goal>
repackage
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
zlt-demo/resources-server-demo/src/main/java/org/zlt/ResourcesServerApp.java
0 → 100644
浏览文件 @
417b4392
package
org.zlt
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
/**
* @author zlt
* @date 2022/6/25
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@SpringBootApplication
public
class
ResourcesServerApp
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
ResourcesServerApp
.
class
,
args
);
}
}
zlt-demo/resources-server-demo/src/main/java/org/zlt/config/MyResourceConfig.java
0 → 100644
浏览文件 @
417b4392
package
org.zlt.config
;
import
com.central.oauth2.common.config.DefaultResourceServerConf
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer
;
/**
* security资源服务器配置
*
* @author zlt
* @version 1.0
* @date 2022/6/25
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Configuration
@EnableResourceServer
public
class
MyResourceConfig
extends
DefaultResourceServerConf
{
}
zlt-demo/resources-server-demo/src/main/java/org/zlt/config/WebMvcConfig.java
0 → 100644
浏览文件 @
417b4392
package
org.zlt.config
;
import
com.central.common.config.DefaultWebMvcConfig
;
import
org.springframework.context.annotation.Configuration
;
/**
* web服务器配置
*
* @author zlt
* @version 1.0
* @date 2022/6/25
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Configuration
public
class
WebMvcConfig
extends
DefaultWebMvcConfig
{
}
zlt-demo/resources-server-demo/src/main/java/org/zlt/controller/TestController.java
0 → 100644
浏览文件 @
417b4392
package
org.zlt.controller
;
import
com.central.common.annotation.LoginUser
;
import
com.central.common.model.SysUser
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* @author zlt
* @date 2022/6/25
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
@RestController
public
class
TestController
{
@GetMapping
(
"/test/auth"
)
public
String
auth
(
@LoginUser
SysUser
user
)
{
return
"auth:"
+
user
.
getUsername
();
}
@GetMapping
(
"/test/notAuth"
)
public
String
notAuth
()
{
return
"notAuth:ok"
;
}
}
zlt-demo/resources-server-demo/src/main/resources/bootstrap.yml
0 → 100644
浏览文件 @
417b4392
server
:
port
:
8093
spring
:
application
:
name
:
zlt-resources-server
main
:
allow-bean-definition-overriding
:
true
zlt
:
security
:
ignore
:
httpUrls
:
>
/test/notAuth
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录