提交 417b4392 编写于 作者: Z zhult13

增加资源服务demo(无网络隔离)

上级 51b69faa
......@@ -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;
}
}
......@@ -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
## 一、说明
资源服务器 demo 样例,以最简化的代码演示如何快速集成一个带鉴权功能的服务,适用于 `无网络隔离` 架构。
> 关于无网络隔离架构的设计可参考文档:[无网络隔离架构](https://www.kancloud.cn/zlt2000/microservices-platform/1153640)
&nbsp;
## 二、启动以下服务
1. zlt-uaa:统一认证中心
2. user-center:用户服务
3. sc-gateway:api网关
4. resources-server-demo
> 环境配置与启动参考文档:https://www.kancloud.cn/zlt2000/microservices-platform/919418
&nbsp;
## 三、测试
### 3.1. 测试接口一
http://localhost:8093/test/notAuth
> 无需token访问
&nbsp;
### 3.2. 测试接口二
http://localhost:8093/test/auth?access_token=xxx
> - xxx 需替换为正确的 access_token
> - 可以通过修改 `bootstrap.yml` 文件中的 `zlt.security.ignore.httpUrls` 参数添加排除校验的url。
&nbsp;
## 四、获取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
<?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
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);
}
}
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 {
}
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 {
}
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";
}
}
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.
先完成此消息的编辑!
想要评论请 注册