Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sureness-shenyu gatewai sample #142

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sample-tom/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ spring:
initialization-mode: always
jpa:
database: mysql
show-sql: true
show-sql: true
4 changes: 3 additions & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<module>solon-plugin-sureness-demo</module>
<module>spring-gateway-sureness</module>
<module>zuul-sureness</module>
<module>shenyu-sureness</module>
<module>shenyu-sureness-sql</module>
<module>sureness-spring-boot-starter-example</module>
<module>micro-profile-sureness</module>
</modules>
Expand All @@ -38,4 +40,4 @@
</dependency>
</dependencies>
</dependencyManagement>
</project>
</project>
55 changes: 55 additions & 0 deletions samples/shenyu-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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">
<parent>
<artifactId>samples</artifactId>
<groupId>com.usthe.sureness</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shenyu-service</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--soul gateway start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-gateway</artifactId>
<version>2.3.0-RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--soul data sync start use websocket-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-websocket</artifactId>
<version>2.3.0-RELEASE</version>
</dependency>
</dependencies>

</project>
18 changes: 18 additions & 0 deletions samples/shenyu-service/src/main/java/com/shenyu/AppBootStrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.shenyu;

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

/**
* @title: AppBootStrap
* @Author hqgordon
* @Date: 2021/8/3 3:15 下午
* @Description:
* @Version 1.0
*/
@SpringBootApplication
public class AppBootStrap {
public static void main(String[] args) {
SpringApplication.run(AppBootStrap.class,args);
}
}
13 changes: 13 additions & 0 deletions samples/shenyu-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spring:
main:
allow-bean-definition-overriding: true
management:
health:
defaults:
enabled: false
shenyu:
sync:
websocket:
urls: ws://localhost:9095/websocket
server:
port: 9196
108 changes: 108 additions & 0 deletions samples/shenyu-sureness-sql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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">
<parent>
<artifactId>samples</artifactId>
<groupId>com.usthe.sureness</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shenyu-sureness-sql</artifactId>

<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--soul gateway start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-gateway</artifactId>
<version>2.3.0-RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--soul data sync start use websocket-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-websocket</artifactId>
<version>2.3.0-RELEASE</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
<version>2.3.0-RELEASE</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-divide</artifactId>
<version>2.3.0-RELEASE</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>io.netty</groupId>-->
<!-- <artifactId>netty-all</artifactId>-->
<!-- <version>4.1.46.Final</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.2.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<!--<scope>runtime</scope>-->
</dependency>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>

</dependencies>
</project>
18 changes: 18 additions & 0 deletions samples/shenyu-sureness-sql/src/main/java/sureness/AppBootSql.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sureness;

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

/**
* @title: AppBootSql
* @Author hqgordon
* @Date: 2021/8/5 9:41 上午
* @Description:
* @Version 1.0
*/
@SpringBootApplication
public class AppBootSql {
public static void main(String[] args) {
SpringApplication.run(AppBootSql.class,args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package sureness.controller;


import com.usthe.sureness.util.JsonWebTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sureness.pojo.dto.Account;
import sureness.pojo.dto.Message;
import sureness.service.AccountService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* @author tomsun28
* @date 00:24 2019-08-01
*/
@RestController
@RequestMapping("/auth")
@Slf4j
public class AccountController {

@Autowired
private AccountService accountService;

private static final String TOKEN_SPLIT = "--";

@PostMapping("/token")
public ResponseEntity<Message> issueJwtToken(@RequestBody @Validated Account account) {
boolean authenticatedFlag = accountService.authenticateAccount(account);
if (!authenticatedFlag) {
Message message = Message.builder()
.errorMsg("username or password not incorrect").build();
if (log.isDebugEnabled()) {
log.debug("account: {} authenticated fail", account);
}
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(message);
}
List<String> ownRole = accountService.loadAccountRoles(account.getUsername());
String jwt = JsonWebTokenUtil.issueJwt(UUID.randomUUID().toString(), account.getUsername(),
"tom-auth-server", 3600L, ownRole);
Map<String, String> responseData = Collections.singletonMap("token", jwt);
Message message = Message.builder().data(responseData).build();
if (log.isDebugEnabled()) {
log.debug("issue token success, account: {} -- token: {}", account, jwt);
}
return ResponseEntity.status(HttpStatus.CREATED).body(message);
}

@PostMapping("/custom/token")
public ResponseEntity<Message> issueCustomToken(@RequestBody @Validated Account account) {
boolean authenticatedFlag = accountService.authenticateAccount(account);
if (!authenticatedFlag) {
Message message = Message.builder()
.errorMsg("username or password not incorrect").build();
if (log.isDebugEnabled()) {
log.debug("account: {} authenticated fail", account);
}
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(message);
}
long refreshPeriodTime = 36000L;
String token = account.getUsername() + TOKEN_SPLIT + System.currentTimeMillis()
+ TOKEN_SPLIT + refreshPeriodTime
+ TOKEN_SPLIT + UUID.randomUUID().toString().replace("-", "");
TokenStorage.addToken(account.getUsername(), token);
Map<String, String> responseData = Collections.singletonMap("customToken", token);
Message message = Message.builder().data(responseData).build();
if (log.isDebugEnabled()) {
log.debug("issue token success, account: {} -- token: {}", account, token);
}
return ResponseEntity.status(HttpStatus.CREATED).body(message);
}

@PostMapping("/register")
public ResponseEntity<Message> accountRegister(@RequestBody @Validated Account account) {
if (accountService.registerAccount(account)) {
Map<String, String> responseData = Collections.singletonMap("success", "sign up success, login after");
Message message = Message.builder().data(responseData).build();
if (log.isDebugEnabled()) {
log.debug("account: {}, sign up success", account);
}
return ResponseEntity.status(HttpStatus.CREATED).body(message);
} else {
Message message = Message.builder()
.errorMsg("username already exist").build();
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sureness.controller;

import com.usthe.sureness.provider.annotation.RequiresRoles;
import com.usthe.sureness.provider.annotation.WithoutAuth;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
* @author tomsun28
* @date 2021/1/19 14:53
*/
@RestController
public class AnnotationController {

@GetMapping("/api/annotation/source1")
@RequiresRoles(roles = {"role1", "role2", "role3"}, mapping = "/api/annotation/source1", method = "get")
public ResponseEntity<String> api1Mock1() {
return ResponseEntity.ok("success");
}

@GetMapping("/api/annotation/source2")
@WithoutAuth(mapping = "/api/annotation/source2", method = "get")
public ResponseEntity<String> api1Mock2() {
return ResponseEntity.ok("success");
}

}
Loading