篡改常识的系统最新章节 网站防篡改设备

什么是Api接口幂等?
【篡改常识的系统最新章节网站防篡改设备】大略来说Api接口幂等在有限的功夫内控制接口考察乞求,控制ip考察度数,不控制平台考察,都不妨拿到数据 。一个接口不不妨反复表单提交,消费一次耗费一次 。
用户场景:同一功夫反复提交屡次乞求 。
什么是数据窜改?
api接口数据窜改,剧本文献,窜改接口参数举行效劳器数据夺取,重要的数据窜改会引导数据库宕机,步调软硬件解体 。
想到这边都领会后盾api接口幂等多要害了吧 。即日给大师讲非对称加密实行后盾接口api幂等 。
实行思绪:jtw+ 考证标识+出面密钥+暂时功夫戳+寄存过时功夫+AES 实行加密算法token 。
实行办法:1,用户登录胜利后,消费加密token寄存redis.
2,下次登录检查token 能否过时,过时请从新登录 。
3,用户登录生存灵验期,不须要登录 。(这边即是单点登录办法)
code中心实行类:
import io.jsonwebtoken.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.Authentication;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.core.userdetails.User;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import javax.annotation.PostConstruct;import javax.servlet.http.HttpServletRequest;import java.util.*;import java.util.stream.Collectors;@Componentpublic class JWTTokenUtils {public static final String AUTHORIZATION_HEADER = "x-token";public static final String AUTHORIZATION_TOKEN = "x-token";private final Logger logger = LoggerFactory.getLogger(JWTTokenUtils.class);private static final String AUTHORITIES_KEY = "auth";private String secretKey; // 出面密钥private long tokenValidityInMilliseconds; // 作废日子private long tokenValidityInMillisecondsForRememberMe; // (记取我)作废日子@PostConstructpublic void init() {this.secretKey = "isoftstone.huwei";int secondIn1day = 1000 * 60 * 60 * 24;this.tokenValidityInMilliseconds = secondIn1day * 2L;this.tokenValidityInMillisecondsForRememberMe = secondIn1day * 7L;}// 创造Tokenpublic String createToken(Authentication authentication, Boolean rememberMe) {String authorities = authentication.getAuthorities().stream() // 获得用户的权力字符串,如 USER,ADMIN.map(GrantedAuthority::getAuthority).collect(Collectors.joining(","));long now = (new Date()).getTime(); // 获得暂时功夫戳Date validity; // 寄存过时功夫if (rememberMe) {validity = new Date(now + this.tokenValidityInMilliseconds);} else {validity = new Date(now + this.tokenValidityInMillisecondsForRememberMe);}return SysConst.SYS_COMPANY_HEAD+"."+ Jwts.builder() // 创造Token令牌.setSubject(authentication.getName()) // 树立面向用户.claim(AUTHORITIES_KEY, authorities) // 增添权力属性.setExpiration(validity) // 树立作废功夫.signWith(SignatureAlgorithm.HS512, secretKey) // 天生出面.compact();}// 获得用户权力public Authentication getAuthentication(String token) {logger.info("JWTTokenUtils Start Get User Auth");// 领会Token的payloadClaims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();Collection<? extends GrantedAuthority> authorities = Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(",")) // 获得用户权力字符串.map(SimpleGrantedAuthority::new).collect(Collectors.toList()); // 将元素变换为GrantedAuthority接口汇合User principal = new User(claims.getSubject(), "", authorities);return new UsernamePasswordAuthenticationToken(principal, null, authorities);}/** * 领会token获得用户源代码 * @param token * @return */public String getAuthSubject(String token) {Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();return claims.getSubject();}public String resolveToken(HttpServletRequest request){String bearerToken = request.getHeader(AUTHORIZATION_HEADER);//从HTTP头部获得TOKENif (StringUtils.hasText(bearerToken) && bearerToken.startsWith(SysConst.SYS_COMPANY_HEAD)){return bearerToken.substring(bearerToken.indexOf(".")+1, bearerToken.length());//归来Token字符串,去除Bearer}String jwt = request.getParameter(AUTHORIZATION_TOKEN);//从乞求参数中获得TOKENif (StringUtils.hasText(jwt) && jwt.startsWith(SysConst.SYS_COMPANY_HEAD)) {return jwt.substring(bearerToken.indexOf(".")+1, jwt.length());}return null;}// 考证Token能否精确public boolean validateToken(String token) {try {Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token); // 经过密钥考证Tokenreturn true;}catch (MalformedJwtException e) { // JWT *** 缺点logger.info("Invalid JWT token.");logger.trace("Invalid JWT token trace: {}", e);} catch (ExpiredJwtException e) { // JWT过时logger.info("Expired JWT token.");logger.trace("Expired JWT token trace: {}", e);} catch (UnsupportedJwtException e) { // 不扶助该JWTlogger.info("Unsupported JWT token.");logger.trace("Unsupported JWT token trace: {}", e);} catch (IllegalArgumentException e) { // 参数缺点特殊logger.info("JWT token compact of handler are invalid.");logger.trace("JWT token compact of handler are invalid trace: {}", e);}catch (SignatureException e) { // 出面特殊logger.info("Invalid JWT signature.");logger.trace("Invalid JWT signature trace: {}", e);}return false;}}redis写入缓存:

推荐阅读