1 Star 2 Fork 1

学习教程 / spring-security-jwt

Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

SpringSecurity的JWT认证

原来项目地址

https://github.com/zhshuixian/learn-spring-boot-2.git

SpringSecurity的相关文章

深入浅出Spring Security(一):三句话解释框架原理

三句话解释框架原理

  1. 整个框架的核心是一个过滤器,这个过滤器名字叫springSecurityFilterChain类型是FilterChainProxy
  2. 核心过滤器里面是过滤器链(列表),过滤器链的每个元素都是一组URL对应一组过滤器
  3. WebSecurity用来创建FilterChainProxy过滤器, HttpSecurity用来创建过滤器链的每个元素。

SpringSecurity的逻辑

TokenFilter是比SpringSecurity先校验的过滤器。TokenFilter的作用就是从token或者session中获取SpringSecurity校验的权限信息。 如果获取不到则看该资源是否支持SpringSecurity匿名访问。

TokenFilter是验证认证的(准确说获取用户信息提交给SpringSecurity的), SpringSecurity则是依据该资源所需要的权限和TokenFilter提交的用户信息来验证该用户是否有授访问该资源。

未授权返回json

SpringSecurity未授权是重定向login,但是前后端分离,不需要重定向,只用返回json数据,告诉登录失效了。

SpringSecurity的用户名和密码

从下面两张图的调用关系来看,principal对应着用户名username,credentials对应着密码password。

img.png img_1.png

整个SpringSecurity的设计应该非常完善,想象之中应该是主要替换jwt的算法和查询数据库登录的部分即可,其他部分应该都是非常晚上的。

org.xian.token.secutiry.UserDetailsServiceImpl.loadUserByUsername 这里查询数据库,获取用户信息

org.xian.token.secutiry.MyAuthenticationSuccessHandler 这里使用jwt返回token信息。

忽略不需要授权和任真、、

处理用户信息

这个非常重要的类,负责了用户信息处理等

org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser

retrieveUser 方法就是根据用户登录输入的用户名去查找用户,如果没找到,就会抛出一个 UsernameNotFoundException,这个异常被 catch 之后,会首先判断是否要隐藏这个异常,如果不隐藏,则原异常原封不动抛出来,如果需要隐藏,则抛出一个新的 BadCredentialsException 异常,BadCredentialsException 异常从字面理解就是密码输入错误的异常。

拓展

  1. 聊一个 GitHub 上开源的 RBAC 权限管理系统,很6! 参考 https://github.com/cjbi/wetech-admin
./gradlew clean bootJar -x test
scp build/libs/spring-security-jwt-token-0.0.1-SNAPSHOT.jar insite@insite.cupb.top:/home/insite/app/admin/

杀死原有进程

ps -ef|grep 'java'|grep 'spring'|grep 'spring-security-jwt'|grep -v 'grep'|awk '{print $2}'|xargs -tI {} kill -9 {}

启动

nohup /opt/jbr/bin/java -Dfile.encoding=utf-8 -Duser.timezone=GMT+08 -jar /home/insite/app/admin/spring-security-jwt-token-0.0.1-SNAPSHOT.jar --logging.level.root=warn -Xmx1G -Xms512M -server -XX:+UseG1GC >>/home/insite/app/admin/spring-security.log  2>&1 &

其他项目可以学习

https://github.com/murraco/spring-boot-jwt

About

Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
Java
1
https://gitee.com/eric-tutorial/spring-security-jwt.git
git@gitee.com:eric-tutorial/spring-security-jwt.git
eric-tutorial
spring-security-jwt
spring-security-jwt
master

Search