怎么做钓鱼网站生成,中山seo外包,上海营销型网站建设,城市建设管理网站登录流程#xff1a; 1、前端调用登录接口#xff0c;往接口里传入账号#xff0c;密码 2、根据账号判断是否有这个用户#xff0c;如果有则继续判断密码是否正确 3、验证成功后#xff0c;则是根据账号#xff0c;登录时间生成token#xff08;用JWT#xff09; 4、将…登录流程 1、前端调用登录接口往接口里传入账号密码 2、根据账号判断是否有这个用户如果有则继续判断密码是否正确 3、验证成功后则是根据账号登录时间生成token用JWT 4、将token存入Redis当中用于token过期策略 5、将token和用户信息返回给前端 6、此后调用后端任何接口都会先判断发来请求里token是否存在、有效(拦截器实现) 7、然后继续接下来的正常调用 具体思路 1、再登录接口中实现账号、密码的验证和token创建 2、实现一个拦截器拦截除登录接口外的其他所有接口 3、再拦截器中从请求头中取出token对其进行正确性的验证 4、然后从redisaccount,token里取出属于这个账号的token如果取的出来则说明这个用户登录状态没有过期然后和取出来的token进行对比如果不一样则说明同一账号再其他地方进行了登录则被挤出登录状态。 5、再这整个判断过程中所产生的异常(没有登录状态不存在这个用户....)都是由全局异常处理器进行捕获然后返回给前端。 pom文件要引的依赖 !--版本-- propertiesjava.version1.8/java.versionlombok.version1.18.12/lombok.versionhutool.version5.8.8/hutool.versionmybatis-plus.version3.5.2/mybatis-plus.versionJWT.version6.0/JWT.version/properties!--JWT--dependencygroupIdcom.nimbusds/groupIdartifactIdnimbus-jose-jwt/artifactIdversion${JWT.version}/version/dependencydependencygroupIdcom.qcby/groupIdartifactIdqcby-common/artifactIdversion1.0-SNAPSHOT/version/dependency!--redis--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion${lombok.version}/version/dependency!--hutool--dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion${hutool.version}/version/dependency
对于token我使用了JWT有一个token的工具类用于token的创建和 验证。(这个类里使用的redisUtil类大家可以从网上随便找一个redis工具类绑定上自己的reids)
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.MACSigner;
import com.nimbusds.jose.crypto.MACVerifier;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import com.qcby.framework.common.exception.ServiceException;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;Component
public class TokenUtil {ResourceRedisUtil redisUtil;/*** 创建秘钥*/private static final byte[] SECRET qngChengBoYa-realtimeWuIngWangJiaQiZhangYv.getBytes();/*** 生成token* param account* return {link String}*/public String buildToken(String account) {try {/*** 1.创建一个32-byte的密匙*/MACSigner macSigner new MACSigner(SECRET);/*** 2. 建立payload 载体*/JWTClaimsSet claimsSet new JWTClaimsSet.Builder().subject(login).claim(ACCOUNT,account).issueTime(new Date()).build();/*** 3. 建立签名*/SignedJWT signedJWT new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);signedJWT.sign(macSigner);/*** 4. 生成token*/String token signedJWT.serialize();redisUtil.setEx(account,token,10,TimeUnit.MINUTES);return token;} catch (KeyLengthException e) {e.printStackTrace();} catch (JOSEException e) {e.printStackTrace();}return null;}/*** 校验token* param token* return*/public boolean verifyToken(String token) {try {SignedJWT jwt SignedJWT.parse(token);JWSVerifier verifier new MACVerifier(SECRET);/*** 校验是否有效*/if (!jwt.verify(verifier)) {return false;}/*** 获取载体中的数据*/String account (String) jwt.getJWTClaimsSet().getClaim(ACCOUNT);//是否有if (Objects.isNull(account)){return false;}/*** 判断redis里是否有account为key的值如果有* 判断token是否和redis里存的是是否一样* 如果不一样说明已经有其他账号登录了则回到登录页面* 如果一样则给token续期*/if (redisUtil.hasKey(account)){String s redisUtil.get(account);if (s.equals(token)){redisUtil.expire(account,10,TimeUnit.MINUTES);return true;}throw new ServiceException(422,有其他设备登录);}} catch (ParseException e) {e.printStackTrace();} catch (JOSEException e) {e.printStackTrace();}return false;}}登录拦截器的实现要先创建一个类并实现HandlerInterceptor这个接口然后再创建一个拦截器的配置类令其实现WebmvcConfigurer这个接口重新addInterceptors方法再这个方法中将之前实现的登录拦截器给注册进去并配置这个拦截器的拦截路径拦截优先级等等。
/*** 请求拦截器* author MI* date 2023/10/03*/
Component
public class LoginInterceptor implements HandlerInterceptor {private static Logger log Logger.getLogger(LoginInterceptor.class);/**** 在请求处理之前进行调用(Controller方法调用之前)param requestparam responseparam handlerreturn booleanthrows Exception*/ResourceTokenUtil tokenUtil;Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {/*** 从请求头中取出token并判断其是否存在和合法*/String token request.getHeader(token);if (token ! null tokenUtil.verifyToken(token)) {return true;}else {throw new ServiceException(100,还未登录);}}/**** 请求处理之后进行调用但是在视图被渲染之前Controller方法调用之后param requestparam responseparam handlerparam modelAndViewthrows Exception*/Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}/**** 整个请求结束之后被调用也就是在DispatchServlet渲染了对应的视图之后执行主要用于进行资源清理工作param requestparam responseparam handlerparam exthrows Exception*/Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}
import javax.annotation.Resource;
Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {ResourceLoginInterceptor loginInterceptor;Overridepublic void addInterceptors(InterceptorRegistry registry) {/*** 登录拦截器* */registry.addInterceptor(loginInterceptor).addPathPatterns(/**).excludePathPatterns(/login).order(1);}
} 全局异常处理器它能捕获到全部的异常前提是要把异常抛出所有异常都会在controller层反应出来因为执行方法的原头在controller。我之前就是用try-catch处理然后一直一直捕获不到然后网上说正常的实现的全局异常器只能捕获到controller层的异常所以拦截器里的异常捕获不到这句话对也不对。拦截器里异常确实捕获不到但只要咱把它抛出去就能再controller层显现了。
具体实现就是我们要加ControllerAdvice注解 ExceptionHandler根据这个注解具体绑定处理哪个异常。
/*** 全局异常处理器* author MI* date 2023/10/02*/
Slf4j
ControllerAdvice
public class GlobalExceptionHandler{/*** 自定义异常拦截器* param req* param e* return {link Result}*/ResponseBodyExceptionHandler(value ServiceException.class)public Result exceptionHandler(HttpServletRequest req, ServiceException e){log.info(发送{}异常,e.getMessage());return Result.getBusinessException(e.getLocalizedMessage(),e.getCode());}ResponseBodyExceptionHandler(value Exception.class)public Result exceptionHandler(HttpServletRequest req, Exception e){log.info(发送{}异常,e.getMessage());return Result.getBusinessException(e.getLocalizedMessage());}
}Server层实现 Service
Slf4j
public class LoginServiceImpl implements ILoginService {ResourceUserMapper userMapper;ResourceTokenUtil tokenUtil;ResourceUserRoleMapper userRoleMapper;/*** 登录实现* param loginDto* return {link LoginVo}*/Overridepublic LoginVo login(LoginDto loginDto) {UserPo userPo userMapper.selectOne(new LambdaQueryWrapperUserPo().eq(UserPo::getAccount, loginDto.getAccount()));if (userPo!null){if (userPo.getPassword().equals(loginDto.getPassword())){/*** 构建token*/String token tokenUtil.buildToken(userPo.getAccount());LoginVo loginVo new LoginVo();loginVo.setToken(token);loginVo.setRoleId(userRoleMapper.selectOne(new LambdaQueryWrapperUserRolePo().eq(UserRolePo::getUserId,userPo.getUserId())).getRoleId());loginVo.setAccount(userPo.getAccount());return loginVo;}else{throw new ServiceException(422,密码错误);}}else {throw new ServiceException(422, 用户不存在);}}
}后面的Controller、Mapper、实体层大家要根据自己的需求字段来进行具体实现我就不贴出来了。 具体用法像这个异常咱直接抛出来就行全局异常处理类都能捕获就不会继续往下执行了它会把报错信息返回给前端
如图所示 文章转载自: http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.nchsz.cn.gov.cn.nchsz.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.fwmln.cn.gov.cn.fwmln.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn