对比两种承载认证信息的方式: session vs token

token验证方案:

session验证方案:

session即会话是将用户信息保存在服务端,根据请求携带的session_id,从服务端存储(通常是redis)里提取出session。token即令牌是将用户信息保存在请求中,不过是加密后的值,在服务端需要对token进行解密,从而提取用户信息。

浅尝JWT(JSON WEB TOKEN)

JWT的使用场景:

authentication: 这是JWT最常见的应用场景。当用户登陆之后,接下来的每个请求都会携带这个JWT信息。单点登录基本上使用它。

information exchange: JWT是一种安全的多点之间信息传输的方式,因为它使用了签名。

JWT的构成:

  • Header
  • Payload
  • Signature

因此一个JWT看上去是长这样的:

xxxxxx.yyyyy.zzzzzz

在此之前,我们将用户登陆后的认证信息保存在SecurityContextHolder中,用户登陆信息保存在ThreadLocal中,理论上不能保证同一用户下一个请求是否被挡。这里将之前提到的Spring security认证方式改成使用token进行认证。

@Slf4j
public class PasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private AuthenticationManager authenticationManager; PasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
} @Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
log.info("hello");
try {
AbstractAuthenticationToken authRequest = buildAuthentication(request);
return this.authenticationManager.authenticate(authRequest);
} catch (Exception failed) {
throw new AuthenticationFailedException("认证失败");
}
} @Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, Authentication authResult) throws IOException, ServletException {
response.addHeader("Authorization", "abcdefg");
} private AbstractAuthenticationToken buildAuthentication(HttpServletRequest request) throws IOException {
LoginInfo loginInfo = new ObjectMapper().readValue(request.getInputStream(), LoginInfo.class);
log.info("login info is " + loginInfo);
return new UsernameAndPasswordAuthenticationToken(loginInfo.getName(), loginInfo.getPassword());
}
}

这个filter extends UsernamePasswordAuthenticationFilter, 默认只对url为/login的请求进行登陆认证。

认证成功之后,在请求头部加token信息。下次请求来的时候,会有一个TokenAuthenticationFilter对token进行验证。

这里随意用了一个token,该token没有携带用户信息,只用来验证是否有权限访问请求。

public class TokenAuthenticationFilter extends BasicAuthenticationFilter {

public TokenAuthenticationFilter(AuthenticationManager authenticationManager) {
super(authenticationManager);
} @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String token = request.getHeader("Authorization");
if(!token.equals("abcdefg")) {
filterChain.doFilter(request, response);
} else {
UsernameAndPasswordAuthenticationToken usernameAndPasswordAuthenticationToken = new UsernameAndPasswordAuthenticationToken();
usernameAndPasswordAuthenticationToken.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(usernameAndPasswordAuthenticationToken);
filterChain.doFilter(request, response);
} }
}

如果我们header里不带token,返回结果是403 forbidden。

如果携带token就能成功访问请求api了。

接下来使用JWT来创建和使用token。

在PasswordAuthenticationFilter认证成功之后,生成一个jwt存在请求返回头里。

在TokenAuthenticationFilter的filter流程中,首先增加jwt校验:

然后就实现了使用jwt的方式认证。

代码git repo: https://github.com/Rying/twitter-clone.git

参考:

https://blog.csdn.net/sxdtzhaoxinguo/article/details/77965226

https://github.com/auth0/java-jwt

Spring security学习笔记(二)的更多相关文章

  1. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  2. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  3. [转]Spring Security学习总结二

    原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/red ...

  4. SpringBoot + Spring Security 学习笔记(二)安全认证流程源码详解

    用户认证流程 UsernamePasswordAuthenticationFilter 我们直接来看UsernamePasswordAuthenticationFilter类, public clas ...

  5. Spring Security学习笔记一

    一.使用Spring Security 1.在pom 文件中添加Spring Security的依赖. <dependency> <groupId>org.springfram ...

  6. Spring Security学习笔记

    Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...

  7. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置

    官方文档参考,5.1.2 中文参考文档,4.1 中文参考文档,4.1 官方文档中文翻译与源码解读 SpringSecurity 核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) ...

  8. Spring Security学习笔记(三)

    之前提到过认证后怎么存放用户信息,令牌token是一种方式,session是另一种方式,这里介绍使用spring session data redis存储httpSession. 添加了以上依赖后,我 ...

  9. Spring Security学习笔记(一)

    认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...

随机推荐

  1. 电脑护眼小软件f.lux

    f.lux这软件用了能不能保护好视力不好说,反正我是用了以后这么多年一直都在用,狠不下心删去.至少安装后能让心里多一些安全感! 以前老控制不住长期坐在电脑前不动,太需要有这类软件来养护.用了没太明显的 ...

  2. PPTP has become obsolete

    https://www.ovpn.com/en/blog/pptp-has-become-obsolete/ PPTP has become obsolete What is PPTP? PPTP s ...

  3. Linux命令 标签: linux 2016-08-01 10:26 508人阅读 评论(0) 收藏

    Linux常用命令 文件.目录的基本操作 ls - 查看文件 cp - 拷贝文件 mv - 移动或重命名文件 rm - 删除文件 touch - 创建空文件或更新文件时间 cd - 改变当前路径 pw ...

  4. easyui学习笔记9—手风琴格子的增,删和选择

    这一篇中我们将看看如何给手风琴动态的增加,删除格子,怎样选择某一个格子的. 1.先看看引用的资源 <link rel="stylesheet" href="jque ...

  5. call/apply

    call与apply都可以改变this指向,但是传参列表不同. call 任何一种方法都可以.call,借用别人函数,自己用. call只需把实参按照形参的个数传进去,apply只能传一个argume ...

  6. win10 x64 注册ZQDeviceOcx.ocx控件

    正常的方式在32位系统下可行, 但是在64位系统下是不可行的. 在64位系统中正确的注册步骤是: 1. 将对应的ocx和dll放到C:\Windows\SysWOW64目录下. 2. 然后找到C:\W ...

  7. websphere 配置库中已存在应用程序,异常处理

    from:http://mengdboy.iteye.com/blog/1677379 出现此问题的原因之一:操作界面上没有卸载完成. 进行一下操作: 1.删除 $WAS_HOME/profiles/ ...

  8. functions and closures are reference types-函数和闭包是引用类型

    Closures Are Reference Types In the example above, incrementBySeven and incrementByTen are constants ...

  9. 【luogu P2919 [USACO08NOV]守护农场Guarding the Farm】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2919 1.搜索的时候分清楚全局变量和局部变量的区别 2.排序优化搜索 #include <cstdio ...

  10. 【luogu P1939 【模板】矩阵加速(数列)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1939 对于矩阵推序列的式子: 由题意知: f[x+1] =1f[x] + 0f[x-1] + 1f[x-2] ...