diff --git a/pom.xml b/pom.xml index 21eb306..9b21f5f 100644 --- a/pom.xml +++ b/pom.xml @@ -30,9 +30,9 @@ shiro-springboot-cas-tmail-9005 shiro-springboot-cas-pac4j-taobao-9006 shiro-springboot-cas-pac4j-tmail-9007 - shiro-springboot-jwt-9008 - shiro-springboot-cas-pac4j-admin-9008 shiro-springboot-admin-9010 + shiro-springboot-admin-jwt-9011 + shiro-sprinboot-proxy @@ -50,6 +50,7 @@ 3.2.0 2.9.9 1.2.5 + 3.10.3 @@ -59,6 +60,11 @@ spring-boot-starter-freemarker 2.2.6.RELEASE + + org.springframework.boot + spring-boot-starter-aop + 2.2.6.RELEASE + org.springframework.boot spring-boot-starter-web @@ -180,6 +186,13 @@ pagehelper-spring-boot-starter ${pagehelper.version} + + + com.auth0 + java-jwt + ${jwt.version} + + diff --git a/shiro-sprinboot-proxy/pom.xml b/shiro-sprinboot-proxy/pom.xml new file mode 100644 index 0000000..db6d9d3 --- /dev/null +++ b/shiro-sprinboot-proxy/pom.xml @@ -0,0 +1,93 @@ + + + + shiro + com.bjmashibing.shiro + 0.0.1-SNAPSHOT + + 4.0.0 + + shiro-sprinboot-proxy + + + + org.springframework.boot + spring-boot-starter-aop + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + com.alibaba + fastjson + + + org.apache.commons + commons-lang3 + + + commons-lang + commons-lang + 2.6 + compile + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/ProxyApplication.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/ProxyApplication.java new file mode 100644 index 0000000..a4e063a --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/ProxyApplication.java @@ -0,0 +1,17 @@ +package com.bjmashibing.shiro; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class ProxyApplication extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(ProxyApplication.class, args); + } + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(ProxyApplication.class); + } +} \ No newline at end of file diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AnnotationProcessorConfiguration.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AnnotationProcessorConfiguration.java new file mode 100644 index 0000000..ba33d39 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AnnotationProcessorConfiguration.java @@ -0,0 +1,25 @@ +package com.bjmashibing.shiro.proxy; + +import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2019/9/27 13:42 + */ +//@Configuration +public class AnnotationProcessorConfiguration { + @Bean + public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){ + return new DefaultAdvisorAutoProxyCreator(); + } + @Bean + public LogAdvisor logAdvisor(){ + LogAdvisor logAdvisor = new LogAdvisor(); + return logAdvisor; + } + +} \ No newline at end of file diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AopLogMethodInterceptor.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AopLogMethodInterceptor.java new file mode 100644 index 0000000..189b945 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/AopLogMethodInterceptor.java @@ -0,0 +1,24 @@ +package com.bjmashibing.shiro.proxy; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +import java.lang.reflect.Method; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2019/9/27 13:43 + */ +public class AopLogMethodInterceptor implements MethodInterceptor { + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Method method = invocation.getThis().getClass().getDeclaredMethod(invocation.getMethod().getName(),invocation.getMethod().getParameterTypes()); +// SysLog sysLog = method.getAnnotation(SysLog.class); +// System.out.println("log: "+sysLog); + System.out.println("======"); + return invocation.proceed(); + } + + +} diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/LogAdvisor.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/LogAdvisor.java new file mode 100644 index 0000000..bccbe06 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/LogAdvisor.java @@ -0,0 +1,58 @@ +package com.bjmashibing.shiro.proxy; + +import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; +import org.springframework.core.annotation.AnnotationUtils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2019/9/27 19:33 + */ +public class LogAdvisor extends StaticMethodMatcherPointcutAdvisor { + private static final Class[] ANNOTATION_CLASSES = new Class[] {SysLog.class}; + public LogAdvisor() { + setAdvice(new AopLogMethodInterceptor()); + } + + @Override + public boolean matches(Method method, Class targetClass) { + Method m = method; + + if ( isAuthzAnnotationPresent(m) ) { + return true; + } + if ( targetClass != null) { + try { + m = targetClass.getMethod(m.getName(), m.getParameterTypes()); + return isAuthzAnnotationPresent(m) || isAuthzAnnotationPresent(targetClass); + } catch (NoSuchMethodException ignored) { + } + } + + return false; + } + + private boolean isAuthzAnnotationPresent(Class targetClazz) { + for( Class annClass : ANNOTATION_CLASSES ) { + Annotation a = AnnotationUtils.findAnnotation(targetClazz, annClass); + if ( a != null ) { + return true; + } + } + return false; + } + + private boolean isAuthzAnnotationPresent(Method method) { + for( Class annClass : ANNOTATION_CLASSES ) { + Annotation a = AnnotationUtils.findAnnotation(method, annClass); + if ( a != null ) { + return true; + } + } + return false; + } +} diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLog.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLog.java new file mode 100644 index 0000000..8485e1d --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLog.java @@ -0,0 +1,14 @@ +package com.bjmashibing.shiro.proxy; + +import java.lang.annotation.*; + +/** + * + * @author sunzhiqiang23 + * @date 2020-06-06 17:40 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface SysLog { +} diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLogAspect.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLogAspect.java new file mode 100644 index 0000000..193d9d4 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/proxy/SysLogAspect.java @@ -0,0 +1,52 @@ +package com.bjmashibing.shiro.proxy; + +import com.alibaba.fastjson.JSON; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import sun.reflect.misc.MethodUtil; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.Method; + + +/** + * 系统日志,切面处理类 + * + * @author chenshun + * @email sunlightcs@gmail.com + * @date 2017年3月8日 上午11:07:35 + */ +@Aspect +@Component +public class SysLogAspect { + + @Pointcut("@annotation(com.bjmashibing.shiro.proxy.SysLog)") + public void logPointCut() { + + } + @Around("logPointCut()") + public Object around(ProceedingJoinPoint point) throws Throwable { + //执行方法 + Object result =null ; + try { + result = point.proceed(); + System.out.println("============="); + //执行时长(毫秒) + }catch (Exception e){ + e.printStackTrace(); + } + return result; + } + +} diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserService.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserService.java new file mode 100644 index 0000000..20e76a1 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserService.java @@ -0,0 +1,12 @@ +package com.bjmashibing.shiro.service; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 17:40 + */ +public interface UserService { + int add(); + int select(); +} diff --git a/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserServiceImpl.java b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserServiceImpl.java new file mode 100644 index 0000000..dd44733 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/java/com/bjmashibing/shiro/service/UserServiceImpl.java @@ -0,0 +1,27 @@ +package com.bjmashibing.shiro.service; + +import com.bjmashibing.shiro.proxy.SysLog; +import org.springframework.stereotype.Service; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 17:41 + */ +@Service +public class UserServiceImpl implements UserService{ + @SysLog + @Override + public int add() { + System.out.println("添加用户"); + select(); + return 0; + } + @SysLog + @Override + public int select() { + System.out.println("查询用户"); + return 0; + } +} diff --git a/shiro-sprinboot-proxy/src/main/resources/application.yml b/shiro-sprinboot-proxy/src/main/resources/application.yml new file mode 100644 index 0000000..623d2e3 --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/resources/application.yml @@ -0,0 +1,16 @@ +# Tomcat +server: + tomcat: + uri-encoding: UTF-8 + max-threads: 1000 + min-spare-threads: 30 + port: 80 +spring: + mvc: + static-path-pattern: /static/** + freemarker: + suffix: .html + request-context-attribute: request + jackson: + time-zone: GMT+8 + date-format: yyyy-MM-dd HH:mm:ss \ No newline at end of file diff --git a/shiro-sprinboot-proxy/src/main/resources/logback-spring.xml b/shiro-sprinboot-proxy/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..18e03ea --- /dev/null +++ b/shiro-sprinboot-proxy/src/main/resources/logback-spring.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/ApplicationTests.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/ApplicationTests.java new file mode 100644 index 0000000..d54f9af --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/ApplicationTests.java @@ -0,0 +1,14 @@ +package com.bjmashibing.shiro; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class ApplicationTests { + + @Test + void contextLoads() { + + } + +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/UserServiceTest.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/UserServiceTest.java new file mode 100644 index 0000000..0adab23 --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/UserServiceTest.java @@ -0,0 +1,27 @@ +package com.bjmashibing.shiro; + +import com.bjmashibing.shiro.service.UserService; +import com.bjmashibing.shiro.service.UserServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 17:43 + */ +@Slf4j +public class UserServiceTest extends ApplicationTests { + + @Autowired + private UserService userService; + @Test + public void add(){ + //userservice 真正运行的时候,是不是代理对象? + userService.add(); + + } +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/Client.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/Client.java new file mode 100644 index 0000000..0d557fa --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/Client.java @@ -0,0 +1,35 @@ +package com.bjmashibing.shiro.proxy; + +import org.junit.jupiter.api.Test; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.NoOp; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 17:16 + */ + +public class Client { + + @Test + public void testJdkProxy(){ + LogService logService = new LogServiceImpl(); + LogInvocationHandler handler = new LogInvocationHandler(logService); + LogService proxy = (LogService)handler.getProxy(); + proxy.log(); + } + + @Test + public void testCglib() { + LogService logService = new LogServiceImpl(); + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(logService.getClass()); + enhancer.setCallback(NoOp.INSTANCE); + //设置类加载器 + enhancer.setClassLoader(logService.getClass().getClassLoader()); + LogServiceImpl proxy = (LogServiceImpl)enhancer.create(); + proxy.log(); + } +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogInvocationHandler.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogInvocationHandler.java new file mode 100644 index 0000000..0109f7c --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogInvocationHandler.java @@ -0,0 +1,31 @@ +package com.bjmashibing.shiro.proxy; + +import org.springframework.aop.interceptor.PerformanceMonitorInterceptor; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 15:57 + */ +public class LogInvocationHandler implements InvocationHandler { + private Object target; + + public LogInvocationHandler(Object target) { + super(); + this.target = target; + } + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Object result = method.invoke(target, args); + return result; + } + + public Object getProxy(){ + return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this); + } +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogService.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogService.java new file mode 100644 index 0000000..2cea9f3 --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogService.java @@ -0,0 +1,11 @@ +package com.bjmashibing.shiro.proxy; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 15:05 + */ +public interface LogService { + void log(); +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogServiceImpl.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogServiceImpl.java new file mode 100644 index 0000000..4d21a3a --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/LogServiceImpl.java @@ -0,0 +1,15 @@ +package com.bjmashibing.shiro.proxy; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-06 15:05 + */ +public class LogServiceImpl implements LogService { + + @Override + public void log() { + System.out.println("记录日志"); + } +} diff --git a/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/MataspaceOOMTest.java b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/MataspaceOOMTest.java new file mode 100644 index 0000000..5d2b0a7 --- /dev/null +++ b/shiro-sprinboot-proxy/src/test/java/com/bjmashibing/shiro/proxy/MataspaceOOMTest.java @@ -0,0 +1,44 @@ +package com.bjmashibing.shiro.proxy; + +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; + +/** + * 〈一句话功能简述〉
+ * JVM参数 + * -XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=12m + * 模拟Metaspace空间溢出,我们不断生成类王空间灌,类占据的空间总是会超过Metaspace指定的空间大小的。 + **/ + +public class MataspaceOOMTest { + static class OOMTest{ + } + public static void main(String[] args) { + int i = 0; //模拟计数多少次以后发生异常 + try { + while(true){ + i++; + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(OOMTest.class); + enhancer.setUseCache(false); + enhancer.setCallback(new MethodInterceptor() { + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + return methodProxy.invoke(o,args); + } + }); + enhancer.create(); + } + + } catch (Throwable throwable) { + System.out.println("*******多少次后发生了异常:" + i); + throwable.printStackTrace(); + } + + + } + +} diff --git a/shiro-springboot-admin-jwt-9011/pom.xml b/shiro-springboot-admin-jwt-9011/pom.xml new file mode 100644 index 0000000..ca852d9 --- /dev/null +++ b/shiro-springboot-admin-jwt-9011/pom.xml @@ -0,0 +1,87 @@ + + + + shiro + com.bjmashibing.shiro + 0.0.1-SNAPSHOT + + 4.0.0 + + shiro-springboot-admin-jwt-9011 + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + com.alibaba + fastjson + + + org.apache.commons + commons-lang3 + + + com.auth0 + java-jwt + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/Base64ConvertUtil.java b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/Base64ConvertUtil.java new file mode 100644 index 0000000..0497eb2 --- /dev/null +++ b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/Base64ConvertUtil.java @@ -0,0 +1,32 @@ +package com.bjmashibing.shiro.framework.util; + +import java.io.UnsupportedEncodingException; +import java.util.Base64; + +/** + * Base64工具 + * @author 孙志强 + * @date 2018/11/05 23:10 + */ +public class Base64ConvertUtil { + /** + * 加密 + * @param str + * @return java.lang.String + */ + public static String encode(String str) throws UnsupportedEncodingException { + byte[] encodeBytes = Base64.getEncoder().encode(str.getBytes("utf-8")); + return new String(encodeBytes); + } + + /** + * 解密 + * @param str + * @return java.lang.String + */ + public static String decode(String str) throws UnsupportedEncodingException { + byte[] decodeBytes = Base64.getDecoder().decode(str.getBytes("utf-8")); + return new String(decodeBytes); + } + +} diff --git a/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/JwtUtil.java b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/JwtUtil.java new file mode 100644 index 0000000..88c251d --- /dev/null +++ b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/JwtUtil.java @@ -0,0 +1,91 @@ +package com.bjmashibing.shiro.framework.util; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTDecodeException; +import com.auth0.jwt.interfaces.DecodedJWT; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import java.io.UnsupportedEncodingException; +import java.util.Date; + +/** + * JWT工具类 + * @author sunzhiqiang23 + * @date 2020/05/29 21:23 + */ +@Slf4j +@Component +public class JwtUtil { + + + /** + * 过期时间改为从配置文件获取 + */ + private static String accessTokenExpireTime ="300"; + + /** + * JWT认证加密私钥(Base64加密) + */ + private static String encryptJWTKey="a30ade645272536288ccae58570738ee"; + + public static final String CLAIM = "account"; + + + /** + * 校验验证帐号加JWT私钥解密 是否正确 + * @param token Token + * @return boolean 是否正确 + */ + public static boolean verify(String token) { + try { + String secret = getClaim(token, CLAIM) + Base64ConvertUtil.decode(encryptJWTKey); + Algorithm algorithm = Algorithm.HMAC256(secret); + JWTVerifier verifier = JWT.require(algorithm).build(); + verifier.verify(token); + return true; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("认证解密异常:" + e.getMessage()); + } + } + + /** + * 获得Token中的信息 + * @param token + * @param claim + * @return java.lang.String + */ + public static String getClaim(String token, String claim) { + try { + DecodedJWT jwt = JWT.decode(token); + return jwt.getClaim(claim).asString(); + } catch (JWTDecodeException e) { + throw new RuntimeException("解密异常:" + e.getMessage()); + } + } + + /** + * 生成签名 + * @param account 帐号 + * @return java.lang.String 返回加密的Token + */ + public static String sign(String account) { + try { + // 帐号加JWT私钥加密 + String secret = account + Base64ConvertUtil.decode(encryptJWTKey); + // 此处过期时间是以毫秒为单位,所以乘以1000 + Date date = new Date(System.currentTimeMillis() + Long.parseLong(accessTokenExpireTime) * 1000); + Algorithm algorithm = Algorithm.HMAC256(secret); + // 附带account帐号信息 + return JWT.create() + .withClaim("account", account) + .withClaim("currentTimeMillis", String.valueOf(System.currentTimeMillis())) + .withExpiresAt(date) + .sign(algorithm); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("加密异常:" + e.getMessage()); + } + } +} diff --git a/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/R.java b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/R.java new file mode 100644 index 0000000..cd44141 --- /dev/null +++ b/shiro-springboot-admin-jwt-9011/src/main/java/com/bjmashibing/shiro/framework/util/R.java @@ -0,0 +1,47 @@ +package com.bjmashibing.shiro.framework.util; + +import java.util.HashMap; + +/** + * 返回数据 + * + * @author 孙志强 + + * @date 2019年12月19日 下午4:59:27 + */ +public class R extends HashMap { + private static final long serialVersionUID = 1L; + + public R() { + put("code", 0); + } + public static R error() { + return error(500, "未知异常,请联系管理员"); + } + + public static R error(String info) { + return error(500, info); + } + + public static R error(int code, String info) { + R r = new R(); + r.put("code", code); + r.put("info", info); + return r; + } + public static R ok(String info) { + R r = new R(); + r.put("info", info); + return r; + } + + public static R ok() { + return new R(); + } + + @Override + public R put(String key, Object value) { + super.put(key, value); + return this; + } +} diff --git a/shiro-springboot-admin-jwt-9011/src/test/java/com/bjmashibing/shiro/framework/util/JwtUtilTest.java b/shiro-springboot-admin-jwt-9011/src/test/java/com/bjmashibing/shiro/framework/util/JwtUtilTest.java new file mode 100644 index 0000000..95559bd --- /dev/null +++ b/shiro-springboot-admin-jwt-9011/src/test/java/com/bjmashibing/shiro/framework/util/JwtUtilTest.java @@ -0,0 +1,39 @@ +package com.bjmashibing.shiro.framework.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + *

+ * + * @author sunzhiqiang23 + * @date 2020-06-01 21:15 + */ +class JwtUtilTest { + + @Test + void verify() { + String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXJyZW50VGltZU1pbGxpcyI6IjE1OTE0NDcyOTU4NTMiLCJleHAiOjE1OTE0NDc1OTUsImFjY291bnQiOiJzeXN0ZW0ifQ._y17zAMGtwGNDTmLNMmaEEGP99zMvognQqwla87174s"; + String account = JwtUtil.getClaim(token, "account"); + boolean system = JwtUtil.verify(token); + System.out.println(system); + + } + + @Test + void getClaim() { + String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXJyZW50VGltZU1pbGxpcyI6IjE1OTE0NDcyOTU4NTMiLCJleHAiOjE1OTE0NDc1OTUsImFjY291bnQiOiJzeXN0ZW0ifQ._y17zAMGtwGNDTmLNMmaEEGP99zMvognQqwla87174s"; + String claim = "account"; + String account = JwtUtil.getClaim(token, claim); + System.out.println(account); + } + + @Test + void sign() { + String account = "system"; + String sign = JwtUtil.sign(account); + System.out.println(sign); + + } +} \ No newline at end of file diff --git a/资料/shiro.emmx b/资料/shiro.emmx index cf17741..5d669b5 100644 Binary files a/资料/shiro.emmx and b/资料/shiro.emmx differ