forked from rongchao/epmet-cloud-rizhao
				
			
				 8 changed files with 343 additions and 6 deletions
			
			
		| @ -0,0 +1,32 @@ | |||||
|  | /** | ||||
|  |  * Copyright 2018 人人开源 http://www.renren.io
 | ||||
|  |  * <p> | ||||
|  |  * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||||
|  |  * use this file except in compliance with the License. You may obtain a copy of | ||||
|  |  * the License at | ||||
|  |  * <p> | ||||
|  |  * http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  |  * <p> | ||||
|  |  * Unless required by applicable law or agreed to in writing, software | ||||
|  |  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||
|  |  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||
|  |  * License for the specific language governing permissions and limitations under | ||||
|  |  * the License. | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | package com.epmet.commons.extappauth.annotation; | ||||
|  | 
 | ||||
|  | import java.lang.annotation.*; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * 需要认证的内部请求 | ||||
|  |  * @Author wxz | ||||
|  |  * @Description | ||||
|  |  * @Date 2020/4/23 16:17 | ||||
|  |  **/ | ||||
|  | @Target(ElementType.METHOD) | ||||
|  | @Retention(RetentionPolicy.RUNTIME) | ||||
|  | @Documented | ||||
|  | public @interface InternalAppRequestAuth { | ||||
|  | 
 | ||||
|  | } | ||||
| @ -0,0 +1,41 @@ | |||||
|  | /** | ||||
|  |  * Copyright (c) 2018 人人开源 All rights reserved. | ||||
|  |  * | ||||
|  |  * https://www.renren.io
 | ||||
|  |  * | ||||
|  |  * 版权所有,侵权必究! | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | package com.epmet.commons.extappauth.jwt; | ||||
|  | 
 | ||||
|  | import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
|  | import org.springframework.context.annotation.Configuration; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * Jwt | ||||
|  |  * | ||||
|  |  * @author Mark sunlightcs@gmail.com | ||||
|  |  * @since 1.0.0 | ||||
|  |  */ | ||||
|  | @Configuration | ||||
|  | @ConfigurationProperties(prefix = "jwt.token") | ||||
|  | public class JwtTokenProperties { | ||||
|  |     private String secret; | ||||
|  |     private int expire; | ||||
|  | 
 | ||||
|  |     public String getSecret() { | ||||
|  |         return secret; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public void setSecret(String secret) { | ||||
|  |         this.secret = secret; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public int getExpire() { | ||||
|  |         return expire; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public void setExpire(int expire) { | ||||
|  |         this.expire = expire; | ||||
|  |     } | ||||
|  | } | ||||
| @ -0,0 +1,130 @@ | |||||
|  | /** | ||||
|  |  * Copyright (c) 2018 人人开源 All rights reserved. | ||||
|  |  * <p> | ||||
|  |  * https://www.renren.io
 | ||||
|  |  * <p> | ||||
|  |  * 版权所有,侵权必究! | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | package com.epmet.commons.extappauth.jwt; | ||||
|  | 
 | ||||
|  | import io.jsonwebtoken.Claims; | ||||
|  | import io.jsonwebtoken.Jwts; | ||||
|  | import io.jsonwebtoken.SignatureAlgorithm; | ||||
|  | import org.joda.time.DateTime; | ||||
|  | import org.slf4j.Logger; | ||||
|  | import org.slf4j.LoggerFactory; | ||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||
|  | import org.springframework.stereotype.Component; | ||||
|  | 
 | ||||
|  | import java.util.Date; | ||||
|  | import java.util.HashMap; | ||||
|  | import java.util.Map; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * Jwt工具类 | ||||
|  |  * | ||||
|  |  * @author Mark sunlightcs@gmail.com | ||||
|  |  * @since 1.0.0 | ||||
|  |  */ | ||||
|  | @Component | ||||
|  | public class JwtTokenUtils { | ||||
|  |     private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtils.class); | ||||
|  | 
 | ||||
|  |     @Autowired | ||||
|  |     private JwtTokenProperties jwtProperties; | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 生成jwt token 弃用 | ||||
|  |      */ | ||||
|  |     @Deprecated | ||||
|  |     public String generateToken(String userId) { | ||||
|  |         return Jwts.builder() | ||||
|  |                 .setHeaderParam("typ", "JWT") | ||||
|  |                 .setSubject(userId) | ||||
|  |                 .setIssuedAt(new Date()) | ||||
|  |                 .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) | ||||
|  |                 .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) | ||||
|  |                 .compact(); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public Claims getClaimByToken(String token) { | ||||
|  |         try { | ||||
|  |             return Jwts.parser() | ||||
|  |                     .setSigningKey(jwtProperties.getSecret()) | ||||
|  |                     .parseClaimsJws(token) | ||||
|  |                     .getBody(); | ||||
|  |         } catch (Exception e) { | ||||
|  |             logger.debug("validate is token error, token = " + token, e); | ||||
|  |             return null; | ||||
|  |         } | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * @return java.util.Date | ||||
|  |      * @param token | ||||
|  |      * @Author yinzuomei | ||||
|  |      * @Description 获取token的有效期截止时间 | ||||
|  |      * @Date 2020/3/18 22:17 | ||||
|  |      **/ | ||||
|  |     public Date getExpiration(String token){ | ||||
|  |         try { | ||||
|  |             return Jwts.parser() | ||||
|  |                     .setSigningKey(jwtProperties.getSecret()) | ||||
|  |                     .parseClaimsJws(token) | ||||
|  |                     .getBody().getExpiration(); | ||||
|  |         } catch (Exception e) { | ||||
|  |             logger.debug("validate is token error, token = " + token, e); | ||||
|  |             return null; | ||||
|  |         } | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * @param map | ||||
|  |      * @return java.lang.String | ||||
|  |      * @Author yinzuomei | ||||
|  |      * @Description 根据app+client+userId生成token | ||||
|  |      * @Date 2020/3/18 22:29 | ||||
|  |      **/ | ||||
|  |     public String createToken(Map<String, Object> map) { | ||||
|  |         return Jwts.builder() | ||||
|  |                 .setHeaderParam("typ", "JWT") | ||||
|  |                 .setClaims(map) | ||||
|  |                 .setIssuedAt(new Date()) | ||||
|  |                 .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) | ||||
|  |                 .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) | ||||
|  |                 .compact(); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * token是否过期 | ||||
|  |      * | ||||
|  |      * @return true:过期 | ||||
|  |      */ | ||||
|  |     public boolean isTokenExpired(Date expiration) { | ||||
|  |         return expiration.before(new Date()); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public static void main(String[] args) { | ||||
|  |         Map<String, Object> map=new HashMap<>(); | ||||
|  |         map.put("app","gov"); | ||||
|  |         map.put("client","wxmp"); | ||||
|  |         map.put("userId","100526ABC"); | ||||
|  |         String tokenStr=Jwts.builder() | ||||
|  |                 .setHeaderParam("typ", "JWT") | ||||
|  |                 .setClaims(map) | ||||
|  |                 .setIssuedAt(new Date()) | ||||
|  |                 .setExpiration(DateTime.now().plusSeconds(604800).toDate()) | ||||
|  |                 .signWith(SignatureAlgorithm.HS512, "7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]") | ||||
|  |                 .compact(); | ||||
|  |         System.out.println(tokenStr); | ||||
|  |         Claims claims= Jwts.parser() | ||||
|  |                 .setSigningKey("7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]") | ||||
|  |                 .parseClaimsJws(tokenStr) | ||||
|  |                 .getBody(); | ||||
|  |         System.out.println("app="+ claims.get("app")); | ||||
|  |         System.out.println("client="+ claims.get("client")); | ||||
|  |         System.out.println("userId="+ claims.get("userId")); | ||||
|  |     } | ||||
|  | 
 | ||||
|  | } | ||||
					Loading…
					
					
				
		Reference in new issue