Initial commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Spring" name="Spring">
|
||||
<configuration />
|
||||
</facet>
|
||||
<facet type="web" name="Web">
|
||||
<configuration>
|
||||
<webroots />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="common" />
|
||||
<orderEntry type="module" module-name="system" />
|
||||
<orderEntry type="module" module-name="excel" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml:4.0.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi:4.0.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-schemas:4.0.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.xmlbeans:xmlbeans:3.0.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-compress:1.18" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.virtuald:curvesapi:1.04" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.auth0:java-jwt:3.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.11.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.11.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.26" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.annotation:javax.annotation-api:1.3.2" level="project" />
|
||||
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.8" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.17" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.17" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.17" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.16.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-jpa:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.2.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.transaction:javax.transaction-api:1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:5.3.9.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: javax.persistence:javax.persistence-api:2.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.javassist:javassist:3.23.1-GA" level="project" />
|
||||
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.9.12" level="project" />
|
||||
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.jboss:jandex:2.0.5.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.0.4.Final" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:2.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-orm:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aspects:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-configuration-processor:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-cache:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="RUNTIME" name="Maven: org.springframework.boot:spring-boot-devtools:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.1.4.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.11.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:2.23.4" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.9.12" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.1.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.6.2" level="project" />
|
||||
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.46" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: net.sf.ehcache:ehcache:2.10.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.26" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.jsoup:jsoup:1.11.3" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.cwhelp.component</groupId>
|
||||
<artifactId>jwt</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>组件:jwt</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.cwhelp</groupId>
|
||||
<artifactId>component</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.cwhelp</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.cwhelp.modules</groupId>
|
||||
<artifactId>system</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- jwt处理框架-->
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.cwhelp.component.jwt.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 忽略jwt权限验证注解(只在拦截的地址内有效)
|
||||
* @author yan.y
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface IgnorePermissions {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.cwhelp.component.jwt.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* jwt权限注解(需要权限)
|
||||
* @author yan.y
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface JwtPermissions {
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.cwhelp.component.jwt.annotation;
|
||||
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.exceptions.TokenExpiredException;
|
||||
import com.cwhelp.common.exception.ResultException;
|
||||
import com.cwhelp.component.jwt.config.properties.JwtProjectProperties;
|
||||
import com.cwhelp.component.jwt.enums.JwtResultEnums;
|
||||
import com.cwhelp.component.jwt.utlis.JwtUtil;
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Jwt权限注解AOP
|
||||
* @author yan.y
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "project.jwt.pattern-anno", havingValue = "true", matchIfMissing = true)
|
||||
public class JwtPermissionsAop {
|
||||
|
||||
@Autowired
|
||||
private JwtProjectProperties jwtProperties;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Pointcut("@annotation(com.cwhelp.component.jwt.annotation.JwtPermissions)")
|
||||
public void jwtPermissions() {}
|
||||
|
||||
@Around("jwtPermissions()")
|
||||
public Object doPermission(ProceedingJoinPoint point) throws Throwable {
|
||||
|
||||
// 获取请求对象头部token数据
|
||||
String token = JwtUtil.getRequestToken(request);
|
||||
|
||||
// 验证token数据是否正确
|
||||
try {
|
||||
JwtUtil.verifyToken(token, jwtProperties.getSecret());
|
||||
} catch (TokenExpiredException e) {
|
||||
throw new ResultException(JwtResultEnums.TOKEN_EXPIRED);
|
||||
} catch (JWTVerificationException e) {
|
||||
throw new ResultException(JwtResultEnums.TOKEN_ERROR);
|
||||
}
|
||||
|
||||
return point.proceed();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.cwhelp.component.jwt.config;
|
||||
|
||||
import com.cwhelp.component.jwt.interceptor.AuthenticationInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* jwt权限配置拦截器
|
||||
* @author yan.y
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "project.jwt.pattern-path", havingValue = "true", matchIfMissing = false)
|
||||
public class JwtInterceptorConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationInterceptor authenticationInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authenticationInterceptor).addPathPatterns("/api/**");
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.cwhelp.component.jwt.config.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* jwt配置项
|
||||
* @author yan.y
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "project.jwt")
|
||||
public class JwtProjectProperties {
|
||||
// jwt秘钥
|
||||
private String secret = "mySecret";
|
||||
// 过期时间(天),默认3天
|
||||
private Integer expired = 3;
|
||||
// 权限模式-路径拦截
|
||||
private boolean patternPath = false;
|
||||
// 权限模式-注解拦截
|
||||
private boolean patternAnno = true;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.cwhelp.component.jwt.controller;
|
||||
|
||||
import com.cwhelp.common.enums.StatusEnum;
|
||||
import com.cwhelp.common.exception.ResultException;
|
||||
import com.cwhelp.common.utils.EncryptUtil;
|
||||
import com.cwhelp.common.utils.ResultVoUtil;
|
||||
import com.cwhelp.common.vo.ResultVo;
|
||||
import com.cwhelp.component.jwt.annotation.IgnorePermissions;
|
||||
import com.cwhelp.component.jwt.config.properties.JwtProjectProperties;
|
||||
import com.cwhelp.component.jwt.enums.JwtResultEnums;
|
||||
import com.cwhelp.component.jwt.utlis.JwtUtil;
|
||||
import com.cwhelp.modules.system.domain.User;
|
||||
import com.cwhelp.modules.system.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 默认登录验证控制器
|
||||
* 说明:默认采用系统用户进行登录验证
|
||||
* @author yan.y
|
||||
*/
|
||||
@RestController
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
private JwtProjectProperties properties;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@IgnorePermissions
|
||||
@PostMapping("/api/auth")
|
||||
public ResultVo auth(String username, String password){
|
||||
// 根据用户名获取系统用户数据
|
||||
User user = userService.getByName(username);
|
||||
if (user == null) {
|
||||
throw new ResultException(JwtResultEnums.AUTH_REQUEST_ERROR);
|
||||
} else if (user.getStatus().equals(StatusEnum.FREEZED.getCode())){
|
||||
throw new ResultException(JwtResultEnums.AUTH_REQUEST_LOCKED);
|
||||
} else {
|
||||
// 对明文密码加密处理
|
||||
String encrypt = EncryptUtil.encrypt(password, user.getSalt());
|
||||
// 判断密码是否正确
|
||||
if (encrypt.equals(user.getPassword())) {
|
||||
String token = JwtUtil.getToken(username, properties.getSecret(), properties.getExpired());
|
||||
return ResultVoUtil.success("登录成功", token);
|
||||
} else {
|
||||
throw new ResultException(JwtResultEnums.AUTH_REQUEST_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.cwhelp.component.jwt.enums;
|
||||
|
||||
import com.cwhelp.common.exception.interfaces.ResultInterface;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* jwt结果集枚举
|
||||
* @author yan.y
|
||||
*/
|
||||
@Getter
|
||||
public enum JwtResultEnums implements ResultInterface {
|
||||
|
||||
/**
|
||||
* token问题
|
||||
*/
|
||||
TOKEN_ERROR(301, "token无效"),
|
||||
TOKEN_EXPIRED(302, "token已过期"),
|
||||
|
||||
/**
|
||||
* 账号问题
|
||||
*/
|
||||
AUTH_REQUEST_ERROR(401, "用户名或密码错误"),
|
||||
AUTH_REQUEST_LOCKED(402, "该账号已被冻结"),
|
||||
;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
JwtResultEnums(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.cwhelp.component.jwt.interceptor;
|
||||
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.exceptions.TokenExpiredException;
|
||||
import com.cwhelp.common.exception.ResultException;
|
||||
import com.cwhelp.component.jwt.annotation.IgnorePermissions;
|
||||
import com.cwhelp.component.jwt.config.properties.JwtProjectProperties;
|
||||
import com.cwhelp.component.jwt.enums.JwtResultEnums;
|
||||
import com.cwhelp.component.jwt.utlis.JwtUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* jwt权限拦截器
|
||||
* @author yan.y
|
||||
*/
|
||||
@Component
|
||||
public class AuthenticationInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Autowired
|
||||
private JwtProjectProperties jwtProperties;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// 如果不是映射到方法直接通过
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 判断请求映射的方式是否忽略权限验证
|
||||
HandlerMethod handlerMethod=(HandlerMethod) handler;
|
||||
Method method=handlerMethod.getMethod();
|
||||
if (method.isAnnotationPresent(IgnorePermissions.class)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 获取请求对象头部token数据
|
||||
String token = JwtUtil.getRequestToken(request);
|
||||
|
||||
// 验证token数据是否正确
|
||||
try {
|
||||
JwtUtil.verifyToken(token, jwtProperties.getSecret());
|
||||
} catch (TokenExpiredException e) {
|
||||
throw new ResultException(JwtResultEnums.TOKEN_EXPIRED);
|
||||
} catch (JWTVerificationException e) {
|
||||
throw new ResultException(JwtResultEnums.TOKEN_ERROR);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.cwhelp.component.jwt.utlis;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.exceptions.TokenExpiredException;
|
||||
import com.cwhelp.common.exception.ResultException;
|
||||
import com.cwhelp.common.utils.HttpServletUtil;
|
||||
import com.cwhelp.common.utils.ToolUtil;
|
||||
import com.cwhelp.component.jwt.enums.JwtResultEnums;
|
||||
import com.cwhelp.modules.system.domain.User;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author yan.y
|
||||
*/
|
||||
public class JwtUtil {
|
||||
|
||||
/**
|
||||
* 生成JwtToken
|
||||
* @param username 用户名
|
||||
* @param secret 秘钥
|
||||
* @param amount 过期天数
|
||||
*/
|
||||
public static String getToken(String username, String secret, int amount){
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
return getToken(user, secret, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成JwtToken
|
||||
* @param user 用户对象
|
||||
* @param secret 秘钥
|
||||
* @param amount 过期天数
|
||||
*/
|
||||
public static String getToken(User user, String secret, int amount){
|
||||
// 过期时间
|
||||
Calendar ca = Calendar.getInstance();
|
||||
ca.add(Calendar.DATE, amount);
|
||||
|
||||
// 随机Claim
|
||||
String random = ToolUtil.getRandomString(6);
|
||||
|
||||
// 创建JwtToken对象
|
||||
String token="";
|
||||
token= JWT.create()
|
||||
.withSubject(user.getUsername()) // 用户名
|
||||
.withIssuedAt(new Date()) // 发布时间
|
||||
.withExpiresAt(ca.getTime()) // 过期时间
|
||||
.withClaim("ran", random) // 自定义随机Claim
|
||||
.sign(getSecret(secret, random));
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求对象中的token数据
|
||||
*/
|
||||
public static String getRequestToken(HttpServletRequest request){
|
||||
// 获取JwtTokens失败
|
||||
String authorization = request.getHeader("Authorization");
|
||||
if (authorization == null || !authorization.startsWith("Bearer ")) {
|
||||
throw new ResultException(JwtResultEnums.TOKEN_ERROR);
|
||||
}
|
||||
return authorization.substring(7);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前token中的用户名
|
||||
*/
|
||||
public static String getSubject(){
|
||||
HttpServletRequest request = HttpServletUtil.getRequest();
|
||||
String token = getRequestToken(request);
|
||||
return JWT.decode(token).getSubject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JwtToken
|
||||
* @param token JwtToken数据
|
||||
* @return true 验证通过
|
||||
* @exception TokenExpiredException Token过期
|
||||
* @exception JWTVerificationException 令牌无效(验证不通过)
|
||||
*/
|
||||
public static void verifyToken(String token, String secret) throws JWTVerificationException {
|
||||
String ran = JWT.decode(token).getClaim("ran").asString();
|
||||
JWTVerifier jwtVerifier = JWT.require(getSecret(secret, ran)).build();
|
||||
jwtVerifier.verify(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Secret混淆数据
|
||||
*/
|
||||
private static Algorithm getSecret(String secret, String random){
|
||||
String salt = "君不见黄河之水天上来,奔流到海不复回。君不见高堂明镜悲白发,朝如青丝暮成雪。";
|
||||
//String salt = "元嘉草草,封狼居胥,赢得仓皇北顾。四十三年,望中犹记,烽火扬州路。可堪回首,佛狸祠下,一片神鸦社鼓。凭谁问、廉颇老矣,尚能饭否?";
|
||||
//String salt = "安能摧眉折腰事权贵,使我不得开心颜。";
|
||||
//String salt = "大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。";
|
||||
return Algorithm.HMAC256(secret + salt + "(ノ ̄▽ ̄)ノ 皮一下" + random);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "project.jwt",
|
||||
"type": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties",
|
||||
"sourceType": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "project.jwt.expired",
|
||||
"type": "java.lang.Integer",
|
||||
"sourceType": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties",
|
||||
"defaultValue": 3
|
||||
},
|
||||
{
|
||||
"name": "project.jwt.pattern-anno",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "project.jwt.pattern-path",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "project.jwt.secret",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "com.cwhelp.component.jwt.config.properties.JwtProjectProperties",
|
||||
"defaultValue": "mySecret"
|
||||
}
|
||||
],
|
||||
"hints": []
|
||||
}
|
||||
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user