栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JWT单点登录,java基础入门答案第二版

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JWT单点登录,java基础入门答案第二版

blb.jwt.pubKeyPath=D:java_codepub.rsa

#私钥路径

blb.jwt.priKeyPath=D:java_codepri.rsa

#cookie名称

blb.jwt.cookieName=token

#cookie过期时间

blb.jwt.expire=30

#cookie生命周期

blb.jwt.cookieMaxAge=1800

UserService

@Service

public class UserServiceImpl extends ServiceImpl implements IUserService {

@Override

public UserInfo login(String username, String password) {

//查询用户

UserInfo user = this.getOne(new QueryWrapper().lambda().eq(UserInfo::getUsername, username));

if(user == null){

return null;

}

//将密码加密加盐后进行匹配

String encrypt = Md5Utils.encrypt(password, user.getSalt());

if(encrypt.equals(user.getPassword())){

return user;

}

return null;

}

}

UserController

@RestController

@RequestMapping("/user")

public class UserController {

@Autowired

private IUserService userService;

@Autowired

private JwtProperties properties;

@PostMapping("/login")

public JsonResult login(HttpServletRequest request, HttpServletResponse response) throws Exception {

String username = request.getParameter(“username”);

String password = request.getParameter(“password”);

UserInfo info = userService.login(username, password);

if(info != null){

//验证通过,将用户加密为token

String token = JwtUtils.generateToken(info, properties.getPrivateKey(), properties.getExpire());

//保存token到cookie中

cookieUtils.setcookie(request,response,properties.getcookieName(),token,

properties.getcookieMaxAge(),null,true);

return new JsonResult<>(1,info);

}

return new JsonResult<>(0,null);

}

}

登录页面

{{msg}}

登录

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

解决cookie写入失败的问题

原因1:出现跨域,导致cookie不能写入

1)CORS的配置

2)axios的配置

原因2:Nginx转发域名不一致的问题

Nginx转发配置 : proxy_set_header Host $host;

原因3:zuul的敏感头过滤

关闭敏感头过滤

解决cookie写入问题后,将公钥复制到网关服务器上,在网关中进行token解析实现统一的访问鉴权

网关判断用户登录状态

1)配置白名单,直接通过不进行登录验证

2)创建过滤器ZuulFilter

3)过滤到白名单就直接放行

4)非白名单的请求,获得cookie中的token,解析token

5)如果解析成功,放行,解析失败,就进行拦截

网关的配置文件工具类

@Data

@Configuration

//读取配置文件的注解

@ConfigurationProperties(prefix = “blb.jwt”)

public class JwtProperties {

private List whiteList;//白名单

private String pubKeyPath;//公钥路径

private String cookieName;//cookie名称

private PublicKey publicKey;//公钥

//在构造方法之后自动执行

@PostConstruct

public void init(){

try {

//读取公钥内容

this.publicKey = RsaUtils.getPublicKey(this.pubKeyPath);

}catch (Exception ex){

ex.printStackTrace();

throw new RuntimeException(ex);

}

}

}

application.properties

白名单

blb.jwt.whiteList=/api/auth-api

公钥路径

blb.jwt.pubKeyPath=D:java_codepub.rsa

cookie名称

blb.jwt.cookieName=token

鉴权过滤器

@Component

public class AuthFilter extends ZuulFilter {

@Autowired

private JwtProperties properties;

@Override

public String filterType() {

//前置过滤器

return “pre”;

}

@Override

public int filterOrder() {

return 99;

}

//是否进行过滤,true过滤(执行run方法),false(跳过run)

@Override

public boolean shouldFilter() {

//读取当前请求的地址

String uri = RequestContext.getCurrentContext().getRequest().getRequestURI();

List whiteList = properties.getWhiteList();

//如果地址以白名单中的地址为开头,就不过滤

for(String str : whiteList){

if(uri.startsWith(str)){

return false;

}

}

//不是白名单就过滤

return true;

}

//过滤逻辑

@Override

public Object run() throws ZuulException {

//先从cookie中读取token

RequestContext currentContext = RequestContext.getCurrentContext();

HttpServletRequest request = currentContext.getRequest();

String token = cookieUtils.getcookievalue(request, properties.getcookieName());

//使用公钥对token进行解析

try {

UserInfo user = JwtUtils.getInfoFromToken(token, properties.getPublicKey());

return user;

}catch (Exception ex){

ex.printStackTrace();

//登录拦截

currentContext.setSendZuulResponse(false);

currentContext.setResponseStatusCode(401);

}

return null;
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/681924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号