Skip to content

Commit

Permalink
support spring boot starter (#120)
Browse files Browse the repository at this point in the history
* add spring boot starter

* fix maven

* Update SurenessFilter.java

Co-authored-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
ChineseTony and tomsun28 authored Jul 9, 2021
1 parent 0444a1c commit fc5d1d6
Show file tree
Hide file tree
Showing 16 changed files with 952 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public class SurenessConstant {
public static final String BEARER = "Bearer";
/** Token auth key **/
public static final String TOKEN = "token";

public static final String JWT = "jwt";
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<module>sample-bootstrap</module>
<module>sample-tom</module>
<module>samples</module>
<module>support</module>
</modules>

<properties>
Expand Down
1 change: 1 addition & 0 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>solon-plugin-sureness-demo</module>
<module>spring-gateway-sureness</module>
<module>zuul-sureness</module>
<module>sureness-spring-boot-starter-example</module>
</modules>

<dependencyManagement>
Expand Down
58 changes: 58 additions & 0 deletions samples/sureness-spring-boot-starter-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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>sureness-spring-boot-starter-example</artifactId>

<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.usthe.sureness</groupId>
<artifactId>sureness-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.usthe.sureness.bootstrap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
* @author wangtao
*/
@SpringBootApplication
@ServletComponentScan
public class BootstrapApplication {

public static void main(String[] args) {
SpringApplication.run(BootstrapApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.usthe.sureness.bootstrap.controller;

import com.usthe.sureness.provider.SurenessAccount;
import com.usthe.sureness.provider.SurenessAccountProvider;
import com.usthe.sureness.provider.ducument.DocumentAccountProvider;
import com.usthe.sureness.util.JsonWebTokenUtil;
import com.usthe.sureness.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
* @author wangtao
*/
@RestController()
public class AccountController {

private static final String APP_ID = "appId";

@Autowired
private SurenessAccountProvider accountProvider;

/**
* login, this provider a get jwt api, convenient to test other api with jwt
* @param requestBody request
* @return response
*
*/
@PostMapping("/api/v1/account/auth")
public ResponseEntity<Object> login(@RequestBody Map<String,String> requestBody) {
if (requestBody == null || !requestBody.containsKey(APP_ID)
|| !requestBody.containsKey("password")) {
return ResponseEntity.badRequest().build();
}
String appId = requestBody.get("appId");
String password = requestBody.get("password");
SurenessAccount account = accountProvider.loadAccount(appId);
if (account == null || account.isDisabledAccount() || account.isExcessiveAttempts()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (account.getPassword() != null) {
if (account.getSalt() != null) {
password = Md5Util.md5(password + account.getSalt());
}
if (!account.getPassword().equals(password)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
// Get the roles the user has - rbac
List<String> roles = account.getOwnRoles();
long refreshPeriodTime = 36000L;
// issue jwt
String jwt = JsonWebTokenUtil.issueJwt(UUID.randomUUID().toString(), appId,
"token-server", refreshPeriodTime >> 1, roles,
null, Boolean.FALSE);
Map<String, String> body = Collections.singletonMap("token", jwt);
return ResponseEntity.ok().body(body);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package com.usthe.sureness.bootstrap.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author wangtao
*/
@RestController
public class SimulateController {

/** access success message **/
public static final String SUCCESS_ACCESS_RESOURCE = "access this resource: %s success";

@GetMapping("/api/v1/source1")
public ResponseEntity<Map<String, String>> api1Mock1(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PostMapping("/api/v1/source1")
public ResponseEntity<Map<String, String>> api1Mock2(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PutMapping("/api/v1/source1")
public ResponseEntity<Map<String, String>> api1Mock3(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@DeleteMapping("/api/v1/source1")
public ResponseEntity<Map<String, String>> api1Mock4(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PatchMapping("/api/v1/source1")
public ResponseEntity<Map<String, String>> api1Mock5(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v1/source2/{var1}/{var2}")
public ResponseEntity<Map<String, String>> api1Mock6(HttpServletRequest request, @PathVariable String var1, @PathVariable Integer var2 ) {
Map<String, String> resultMap = new HashMap<>(3);
resultMap.putAll(getResponseMap(request));
if (!StringUtils.isEmpty(var1)) {
resultMap.put("var1", var1);
}
if (Objects.nonNull(var2)) {
resultMap.put("var2", String.valueOf(var2));
}
return ResponseEntity.ok(resultMap);
}

@PostMapping("/api/v1/source2/{var1}")
public ResponseEntity<Map<String, String>> api1Mock7(HttpServletRequest request, @PathVariable String var1) {
Map<String, String> resultMap = new HashMap<>(2);
resultMap.putAll(getResponseMap(request));
if (!StringUtils.isEmpty(var1)) {
resultMap.put("var1", var1);
}
return ResponseEntity.ok(resultMap);
}

@GetMapping("/api/v1/source2")
public ResponseEntity<Map<String, String>> api1Mock8(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PatchMapping("/api/v1/source2")
public ResponseEntity<Map<String, String>> api1Mock10(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v1/source3")
public ResponseEntity<Map<String, String>> api1Mock11(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PostMapping("/api/v1/source3")
public ResponseEntity<Map<String, String>> api1Mock12(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PutMapping("/api/v1/source3")
public ResponseEntity<Map<String, String>> api1Mock13(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@DeleteMapping("/api/v1/source3")
public ResponseEntity<Map<String, String>> api1Mock14(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v2/source3/{var1}")
public ResponseEntity<Map<String, String>> api1Mock15(HttpServletRequest request, @PathVariable String var1) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v2/source4")
public ResponseEntity<Map<String, String>> api2Mock16(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PostMapping("/api/v2/source4")
public ResponseEntity<Map<String, String>> api2Mock17(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PutMapping("/api/v2/source4")
public ResponseEntity<Map<String, String>> api2Mock18(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@DeleteMapping("/api/v2/source4")
public ResponseEntity<Map<String, String>> api2Mock19(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PatchMapping("/api/v2/source4")
public ResponseEntity<Map<String, String>> api2Mock20(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v2/source5")
public ResponseEntity<Map<String, String>> api2Mock21(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PostMapping("/api/v2/source5")
public ResponseEntity<Map<String, String>> api2Mock22(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@DeleteMapping("/api/v2/source5")
public ResponseEntity<Map<String, String>> api2Mock23(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PutMapping("/api/v2/source5")
public ResponseEntity<Map<String, String>> api2Mock24(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PatchMapping("/api/v2/source5")
public ResponseEntity<Map<String, String>> api2Mock25(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@GetMapping("/api/v2/source6")
public ResponseEntity<Map<String, String>> api2Mock26(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

@PostMapping("/api/v2/source6")
public ResponseEntity<Map<String, String>> api2Mock27(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}
@DeleteMapping("/api/v2/source6")
public ResponseEntity<Map<String, String>> api2Mock28(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}
@PutMapping("/api/v2/source6")
public ResponseEntity<Map<String, String>> api2Mock29(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}
@PatchMapping("/api/v2/source6")
public ResponseEntity<Map<String, String>> api2Mock30(HttpServletRequest request) {
return ResponseEntity.ok(getResponseMap(request));
}

/**
* get the response map data from request
* @param request http request
* @return map data
*/
private Map<String, String> getResponseMap(HttpServletRequest request) {
StringBuilder builder = new StringBuilder();
String requestUri = request.getRequestURI();
builder.append(requestUri);
builder.append("--");
String requestType = request.getMethod();
builder.append(requestType);
builder.append("--");
return Collections.singletonMap("result", String.format(SUCCESS_ACCESS_RESOURCE, builder.toString()));
}
}
Loading

0 comments on commit fc5d1d6

Please sign in to comment.