diff --git a/pom.xml b/pom.xml index 59e3667..4ab510d 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ netstate-proc 17 + 1.5.5.Final @@ -48,6 +49,18 @@ 1.11.0 + + com.auth0 + java-jwt + 4.4.0 + + + + org.mapstruct + mapstruct + ${org.mapstruct.version} + + @@ -64,6 +77,22 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + + diff --git a/src/main/java/com/wuyiqi/netstateproc/config/NetStateRealm.java b/src/main/java/com/wuyiqi/netstateproc/config/NetStateRealm.java deleted file mode 100644 index a65d963..0000000 --- a/src/main/java/com/wuyiqi/netstateproc/config/NetStateRealm.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.wuyiqi.netstateproc.config; - -import org.apache.shiro.authc.AuthenticationException; -import org.apache.shiro.authc.AuthenticationInfo; -import org.apache.shiro.authc.AuthenticationToken; -import org.apache.shiro.realm.Realm; - -public class NetStateRealm implements Realm { - @Override - public String getName() { - return null; - } - - @Override - public boolean supports(AuthenticationToken token) { - return false; - } - - @Override - public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { - return null; - } -} diff --git a/src/main/java/com/wuyiqi/netstateproc/config/shiro/JwtToken.java b/src/main/java/com/wuyiqi/netstateproc/config/shiro/JwtToken.java new file mode 100644 index 0000000..325896a --- /dev/null +++ b/src/main/java/com/wuyiqi/netstateproc/config/shiro/JwtToken.java @@ -0,0 +1,47 @@ +package com.wuyiqi.netstateproc.config.shiro; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTVerificationException; +import com.auth0.jwt.interfaces.JWTVerifier; +import com.auth0.jwt.interfaces.Payload; +import com.wuyiqi.netstateproc.convert.JwtConvert; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.crypto.hash.Sha256Hash; +import org.mapstruct.factory.Mappers; + +import java.time.Instant; +import java.util.List; + +@Data +public class JwtToken implements AuthenticationToken { + + private String token; + + private String secret; + + private static final JwtConvert jwtConvert = Mappers.getMapper(JwtConvert.class); + + @Override + public String getPrincipal() { + return JWT.decode(token).getSubject(); + } + + @Override + public Payload getCredentials() throws JWTVerificationException { + Algorithm algorithm = Algorithm.HMAC256(Sha256Hash.toString(secret.getBytes())); + JWTVerifier verifier = JWT.require(algorithm).build(); + return verifier.verify(token); + } + + public static String generate(String subject, Long expire, List rules, String secret) { + Algorithm algorithm = Algorithm.HMAC256(Sha256Hash.toString(secret.getBytes())); + Claims claims = new Claims(subject, Instant.now().plusSeconds(expire), Instant.now(), rules); + return JWT.create().withPayload(jwtConvert.conv2Map(claims)).sign(algorithm); + } + + public record Claims(String subject, Instant expiresAt, Instant issuedAt, List roles) {} + +} diff --git a/src/main/java/com/wuyiqi/netstateproc/config/shiro/NetStateRealm.java b/src/main/java/com/wuyiqi/netstateproc/config/shiro/NetStateRealm.java new file mode 100644 index 0000000..52af205 --- /dev/null +++ b/src/main/java/com/wuyiqi/netstateproc/config/shiro/NetStateRealm.java @@ -0,0 +1,22 @@ +package com.wuyiqi.netstateproc.config.shiro; + +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.realm.Realm; +import org.apache.shiro.subject.PrincipalCollection; + +public class NetStateRealm extends AuthorizingRealm { + + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { + return null; + } + + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { + return null; + } +} diff --git a/src/main/java/com/wuyiqi/netstateproc/config/ShiroConfig.java b/src/main/java/com/wuyiqi/netstateproc/config/shiro/ShiroConfig.java similarity index 93% rename from src/main/java/com/wuyiqi/netstateproc/config/ShiroConfig.java rename to src/main/java/com/wuyiqi/netstateproc/config/shiro/ShiroConfig.java index fd77142..fb4db93 100644 --- a/src/main/java/com/wuyiqi/netstateproc/config/ShiroConfig.java +++ b/src/main/java/com/wuyiqi/netstateproc/config/shiro/ShiroConfig.java @@ -1,4 +1,4 @@ -package com.wuyiqi.netstateproc.config; +package com.wuyiqi.netstateproc.config.shiro; import org.apache.catalina.Realm; import org.apache.shiro.spring.config.ShiroAnnotationProcessorConfiguration; diff --git a/src/main/java/com/wuyiqi/netstateproc/convert/JwtConvert.java b/src/main/java/com/wuyiqi/netstateproc/convert/JwtConvert.java new file mode 100644 index 0000000..a59fda0 --- /dev/null +++ b/src/main/java/com/wuyiqi/netstateproc/convert/JwtConvert.java @@ -0,0 +1,11 @@ +package com.wuyiqi.netstateproc.convert; + +import com.wuyiqi.netstateproc.config.shiro.JwtToken; +import org.mapstruct.Mapper; + +import java.util.Map; + +@Mapper +public interface JwtConvert { + Map conv2Map(JwtToken.Claims claims); +}