parent
867d84780d
commit
3cf54ef4c2
@ -0,0 +1,93 @@
|
|||||||
|
<?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>shiro</artifactId>
|
||||||
|
<groupId>com.bjmashibing.shiro</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>shiro-sprinboot-proxy</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-lang</groupId>
|
||||||
|
<artifactId>commons-lang</artifactId>
|
||||||
|
<version>2.6</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<fork>true</fork>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<!-- 跳过单元测试 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- put your configurations here -->
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> </p>
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package com.bjmashibing.shiro.proxy;
|
||||||
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> </p>
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> </p>
|
||||||
|
*
|
||||||
|
* @author sunzhiqiang23
|
||||||
|
* @date 2019/9/27 19:33
|
||||||
|
*/
|
||||||
|
public class LogAdvisor extends StaticMethodMatcherPointcutAdvisor {
|
||||||
|
private static final Class<? extends Annotation>[] 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<? extends Annotation> annClass : ANNOTATION_CLASSES ) {
|
||||||
|
Annotation a = AnnotationUtils.findAnnotation(targetClazz, annClass);
|
||||||
|
if ( a != null ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAuthzAnnotationPresent(Method method) {
|
||||||
|
for( Class<? extends Annotation> annClass : ANNOTATION_CLASSES ) {
|
||||||
|
Annotation a = AnnotationUtils.findAnnotation(method, annClass);
|
||||||
|
if ( a != null ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 {
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.bjmashibing.shiro.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @author sunzhiqiang23
|
||||||
|
* @date 2020-06-06 17:40
|
||||||
|
*/
|
||||||
|
public interface UserService {
|
||||||
|
int add();
|
||||||
|
int select();
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package com.bjmashibing.shiro.service;
|
||||||
|
|
||||||
|
import com.bjmashibing.shiro.proxy.SysLog;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
|
||||||
|
<configuration scan="true" scanPeriod="10 seconds">
|
||||||
|
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||||
|
|
||||||
|
<logger name="com.bjmashibing.shiro" level="debug"/>
|
||||||
|
</configuration>
|
||||||
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package com.bjmashibing.shiro.proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @author sunzhiqiang23
|
||||||
|
* @date 2020-06-06 15:05
|
||||||
|
*/
|
||||||
|
public interface LogService {
|
||||||
|
void log();
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package com.bjmashibing.shiro.proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @author sunzhiqiang23
|
||||||
|
* @date 2020-06-06 15:05
|
||||||
|
*/
|
||||||
|
public class LogServiceImpl implements LogService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log() {
|
||||||
|
System.out.println("记录日志");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,87 @@
|
|||||||
|
<?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>shiro</artifactId>
|
||||||
|
<groupId>com.bjmashibing.shiro</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>shiro-springboot-admin-jwt-9011</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.auth0</groupId>
|
||||||
|
<artifactId>java-jwt</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<fork>true</fork>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<!-- 跳过单元测试 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- put your configurations here -->
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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<String, Object> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package com.bjmashibing.shiro.framework.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p></p>
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in new issue