diff --git a/samples/solon-sureness/pom.xml b/samples/solon-sureness/pom.xml index 2efc5dbe8f6ed19fcf87bc55a7ed64956e94a28e..122064e1d3e9282c168acdf615d572811623ecde 100644 --- a/samples/solon-sureness/pom.xml +++ b/samples/solon-sureness/pom.xml @@ -14,24 +14,14 @@ 8 8 + 1.3.33 - - - - org.noear - solon-parent - 1.3.33 - pom - import - - - - org.noear - solon-web + solon + ${solon.ver} com.usthe.sureness diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/Application.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/Application.java index 383a02c7a981f4dcdd57eb96b70a76b382d2c8a4..dc7922c36ddbe1055a68f579bcef0bf6f98fe9ca 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/Application.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/Application.java @@ -17,24 +17,25 @@ import java.util.UUID; */ public class Application { - public static void main(String[] args){ - SolonApp app = Solon.start(Application.class, args); + public static void main(String[] args) { + Solon.start(Application.class, args, app -> { + // issue jwt rest api + app.get("/auth/token", ctx -> { + SubjectSum subjectSum = SurenessContextHolder.getBindSubject(); - // issue jwt rest api - app.get("/auth/token", ctx -> { - SubjectSum subjectSum = SurenessContextHolder.getBindSubject(); - if (subjectSum == null) { - RenderManager.global.render("Please auth!", ctx); - } else { - String principal = (String) subjectSum.getPrincipal(); - List roles = (List) subjectSum.getRoles(); - // issue jwt - String jwt = JsonWebTokenUtil.issueJwt(UUID.randomUUID().toString(), principal, - "token-server", 3600L, roles); - RenderManager.global.render(jwt, ctx); - } - }); + if (subjectSum == null) { + RenderManager.global.render("Please auth!", ctx); + } else { + String principal = (String) subjectSum.getPrincipal(); + List roles = (List) subjectSum.getRoles(); + // issue jwt + String jwt = JsonWebTokenUtil.issueJwt(UUID.randomUUID().toString(), principal, + "token-server", 3600L, roles); + RenderManager.global.render(jwt, ctx); + } + }); - app.after("/**", context -> SurenessContextHolder.unbindSubject()); + app.after("/**", context -> SurenessContextHolder.unbindSubject()); + }); } } diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessConfiguration.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessConfiguration.java index 0404430939d3954886f6f100cc58898d9ca260a0..5983513ae314a2df52afb3e16c04f2fa71a5fb1f 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessConfiguration.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessConfiguration.java @@ -59,7 +59,9 @@ public class SurenessConfiguration { DigestProcessor digestProcessor = new DigestProcessor(); digestProcessor.setAccountProvider(accountProvider); processorList.add(digestProcessor); + DefaultProcessorManager processorManager = new DefaultProcessorManager(processorList); + if (logger.isDebugEnabled()) { logger.debug("DefaultProcessorManager init"); } @@ -95,6 +97,4 @@ public class SurenessConfiguration { logger.debug("SurenessSecurityManager init"); } } - - } diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessFilterExample.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessFilterExample.java index 9e4aff3f2949957d4adf020acd28c4b1e247e428..1a8130b957000b984ffa366b81556ebb1d657d37 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessFilterExample.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/SurenessFilterExample.java @@ -40,7 +40,7 @@ public class SurenessFilterExample implements Filter { logger.debug("this request is illegal"); responseWrite(ctx, 401, e1.getMessage(), null); return; - } catch (DisabledAccountException | ExcessiveAttemptsException e2 ) { + } catch (DisabledAccountException | ExcessiveAttemptsException e2) { logger.debug("the account is disabled"); responseWrite(ctx, 401, e2.getMessage(), null); return; @@ -64,17 +64,18 @@ public class SurenessFilterExample implements Filter { /** * write response data - * @param context context + * + * @param ctx ctx * @param statusCode statusCode - * @param message message + * @param message message */ - private void responseWrite(Context context, int statusCode, - String message, Map headers) throws Throwable { - context.statusSet(statusCode); + private void responseWrite(Context ctx, int statusCode, + String message, Map headers) throws Throwable { + ctx.statusSet(statusCode); if (headers != null) { - headers.forEach(context::headerAdd); + headers.forEach(ctx::headerAdd); } - RenderManager.global.render(Collections.singletonMap("message", message), context); - } + ctx.render(Collections.singletonMap("message", message)); + } } diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/BasicSubjectSolonCreator.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/BasicSubjectSolonCreator.java index 23a20d9f77904b35e2aea71c5edace59830544e2..dd52a6f426b69309538737c6af9ddfd20569c7d6 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/BasicSubjectSolonCreator.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/BasicSubjectSolonCreator.java @@ -41,6 +41,7 @@ public class BasicSubjectSolonCreator implements SubjectCreate { if (authorization == null) { return null; } + //basic auth String basicAuth = authorization.replace(BASIC, "").trim(); basicAuth = new String(Base64.getDecoder().decode(basicAuth), StandardCharsets.UTF_8); @@ -51,6 +52,7 @@ public class BasicSubjectSolonCreator implements SubjectCreate { } return null; } + String username = auth[0]; if (username == null || "".equals(username)) { if (logger.isInfoEnabled()) { @@ -58,11 +60,13 @@ public class BasicSubjectSolonCreator implements SubjectCreate { } return null; } + String password = auth[1]; String remoteHost = ((Context) context).ip(); String requestUri = ((Context) context).path(); String requestType = ((Context) context).method(); String targetUri = requestUri.concat("===").concat(requestType).toLowerCase(); + return PasswordSubject.builder(username, password) .setRemoteHost(remoteHost) .setTargetResource(targetUri) diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/DigestSubjectSolonCreator.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/DigestSubjectSolonCreator.java index 9b59cf1e2066d28ccd52deb066b997ba04922a0f..92f2672c0718c7cab5245e03fd30e3dc21ab6721 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/DigestSubjectSolonCreator.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/DigestSubjectSolonCreator.java @@ -51,8 +51,10 @@ public class DigestSubjectSolonCreator implements SubjectCreate { } else { // digest auth String digestAuth = authorization.replace(SurenessConstant.DIGEST, "").trim(); + try { final Map digestMap = new HashMap<>(8); + Arrays.stream(digestAuth.split(",")).forEach(auth -> { String[] tmpArr = auth.trim().split("="); if (tmpArr.length == FILED_SIZE) { @@ -63,6 +65,7 @@ public class DigestSubjectSolonCreator implements SubjectCreate { digestMap.put(tmpArr[0].trim(), authValue); } }); + String username = digestMap.get(USERNAME); String response = digestMap.get(RESPONSE); String realm = digestMap.get(REALM); @@ -71,11 +74,13 @@ public class DigestSubjectSolonCreator implements SubjectCreate { String nc = digestMap.get(NC); String cNonce = digestMap.get(CNONCE); String qop = digestMap.get(QOP); + if (username == null || response == null || realm == null || uri == null || nonce == null || nc == null || cNonce == null) { logger.debug("can not create digest subject due some need field is null"); return null; } + String remoteHost = ((Context) context).ip(); String requestUri = ((Context) context).path(); String requestType = ((Context) context).method(); @@ -89,7 +94,6 @@ public class DigestSubjectSolonCreator implements SubjectCreate { logger.info("create digest subject error happen, due {}", e.getMessage(), e); return null; } - } } } \ No newline at end of file diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/JwtSubjectSolonCreator.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/JwtSubjectSolonCreator.java index 4df7732520cae2ee8069f10f092f6631d9e52fcc..d922f152e4d032b7413b92c5d307bd48e8c92bb8 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/JwtSubjectSolonCreator.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/JwtSubjectSolonCreator.java @@ -39,6 +39,7 @@ public class JwtSubjectSolonCreator implements SubjectCreate { @Override public Subject createSubject(Object context) { String authorization = ((Context)context).header(AUTHORIZATION); + if (authorization != null && authorization.startsWith(BEARER)) { // jwt token String jwtValue = authorization.replace(BEARER, "").trim(); @@ -48,15 +49,18 @@ public class JwtSubjectSolonCreator implements SubjectCreate { } return null; } + String remoteHost = ((Context) context).ip(); String requestUri = ((Context) context).path(); String requestType = ((Context) context).method(); String targetUri = requestUri.concat("===").concat(requestType.toLowerCase()); + return JwtSubject.builder(jwtValue) .setRemoteHost(remoteHost) .setTargetResource(targetUri) .build(); } + return null; } } \ No newline at end of file diff --git a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/NoneSubjectSolonCreator.java b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/NoneSubjectSolonCreator.java index 9a256863f110f9fd8ed0ee85eb185be7e5f6af7d..ff2ee08aca50e5324b1edcdd1043236cbdae9ff3 100644 --- a/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/NoneSubjectSolonCreator.java +++ b/samples/solon-sureness/src/main/java/com/usthe/sureness/solon/support/NoneSubjectSolonCreator.java @@ -24,6 +24,7 @@ public class NoneSubjectSolonCreator implements SubjectCreate { String requestUri = ((Context) context).path(); String requestType = ((Context) context).method(); String targetUri = requestUri.concat("===").concat(requestType).toLowerCase(); + return NoneSubject.builder().setRemoteHost(remoteHost) .setTargetUri(targetUri).build(); }