spring security 源码解析
最近闲暇时间有空来看下spring secrity ,spring secrity 有很多种方式方式,那么我只看了通过数据库获取信息校验,若有什么不妥请及时告知我
spring secrity 第一个入口是一个filter 拦截请求
public Authentication attemptAuthentication(HttpServletRequest request,HttpServletResponse response) throws AuthenticationException {if (postOnly && !request.getMethod().equals("POST")) {throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());}String username = obtainUsername(request);String password = obtainPassword(request);if (username == null) {username = "";}if (password == null) {password = "";}username = username.trim();UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);// Allow subclasses to set the "details" propertysetDetails(request, authRequest);return this.getAuthenticationManager().authenticate(authRequest);}
attemptAuthentication封装好请求对应的账号(principal)密码(credentials)到UsernamePasswordAuthenticationToken这个实体中信息后调用AuthenticationManager的authenticate方法 其中AuthenticationManager由具体的实现类ProviderManager,实际上我们的校验逻辑就在这里
for (AuthenticationProvider provider : getProviders()) {if (!provider.supports(toTest)) {continue;}if (debug) {logger.debug("Authentication attempt using "+ provider.getClass().getName());}try {result = provider.authenticate(authentication);if (result != null) {copyDetails(authentication, result);break;}}
获取具体的AuthenticationProvider进行认证点进去继续看authenticate方法中通过UserDetailsService获取数据库的用户信息
UserDetails user = this.userCache.getUserFromCache(username);if (user == null) {cacheWasUsed = false;try {user = retrieveUser(username,(UsernamePasswordAuthenticationToken) authentication);}catch (UsernameNotFoundException notFound) {logger.debug("User '" + username + "' not found");if (hideUserNotFoundExceptions) {throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials","Bad credentials"));}else {throw notFound;}}
try {preAuthenticationChecks.check(user);additionalAuthenticationChecks(user,(UsernamePasswordAuthenticationToken) authentication);}
数据库的信息以及请求的用户密码都存在了,那么就可以进行校验了
String presentedPassword = authentication.getCredentials().toString();if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {logger.debug("Authentication failed: password does not match stored value");throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials","Bad credentials"));}
这里就看到了进行密码校验,那么身份认证这么搞的,权限认证呢,权限认证是通过Aop实现
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface PreAuthorize {/*** @return the Spring-EL expression to be evaluated before invoking the protected* method*/String value();
}
spring security 源码解析相关推荐
- Spring Security源码解析(一)——认证和鉴权
目录 认证过程 AuthenticationManager Authentication AbstractAuthenticationToken UsernamePasswordAuthenticat ...
- Spring Security源码解析(二)——引入
目录 Spring Security的引入 AuthenticationConfiguration WebSecurityConfiguration 引入 FilterChain. 设置FilterC ...
- Spring Security源码解析(四)—— 过滤器
目录 FilterChainProxy 属性 构造函数 执行Filter 获取Filter VirtualFilterChain 默认过滤器 默认Filter WebAsyncManagerInteg ...
- Spring Security源码解析(三)—— HttpSecurity
目录 SecurityConfigurerAdapter AbstractHttpConfigurer AnonymousConfigurer AbstractAuthenticationFilter ...
- spring事务源码解析
前言 在spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!最后遗留了一个问题:spring是怎么样保证事务一致性的? 当然,spring事务内容挺多的,如果都要讲的话要花很长时间, ...
- spring boot 源码解析23-actuate使用及EndPoint解析
前言 spring boot 中有个很诱人的组件–actuator,可以对spring boot应用做监控,只需在pom文件中加入如下配置即可: <dependency><group ...
- 【若依】开源框架学习笔记 07 - 登录认证流程(Spring Security 源码)
文章目录 一.概述 二.登录过程代码实现 三.用户验证流程(Spring Security 源码) 1.处理用户认证逻辑过滤器 `UsernamePasswordAuthenticationFilte ...
- Spring AOP源码解析-拦截器链的执行过程
一.简介 在前面的两篇文章中,分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在得到了 bean 的代理对象,且通知也以合适的方式插在了目标方 ...
- Spring AOP源码解析——AOP动态代理原理和实现方式
2019独角兽企业重金招聘Python工程师标准>>> Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和 ...
- Spring Session源码解析
AbstractHttpSessionApplicationInitializer,很明显它是一个初始化的类,它是一个抽象类,可以理解为一个公用的基类,然后看一下onStartup这个方法,最主要的方 ...
最新文章
- 小米面试题:合并二叉树
- OC本学习笔记Foundatio框架集
- e300氛围灯哪里调节_奥迪Q5L安装原厂32色20灯源氛围灯
- .net bootstrap 下拉树状选择框_Bootstrap搭建图书管理系统
- 企业服务总线需求说明
- JSP PO VO BO DTO POJO DAO解释
- mysql身份验证失败_SMTP身份验证失败PAM-MySQL无法进行身份验证
- 如何判断Intent有没有对应的Activity去处理?
- 【炮灰模型】 让数学建模来控制你的爱情,测测你是否是那个炮灰
- 离线网页地图开发-详细开发过程
- 苹果官网首页页面设计
- 在编写网站的时候,给一个div设置背景图片,怎么让这个图片随着div大小的改变,而等比例的缩放。
- Linux命令--god
- 异常检测综述(Anomaly Detection: A Survey)
- 一点资讯app signature
- 二元灰狼优化(BGWO)应用于特征选择任务(Matlab代码实现)
- 网易2016笔试(3)
- hdu 2066 一个人的旅行 (多源最短路 )
- 嵌入式未来趋势是什么?
- 解决联想小新air14装虚拟机镜像时蓝屏问题
热门文章
- 小米10获取root权限_oppo A59s (全网通)一键ROOT获取ROOT权限
- 教你快速使用VSCode编写HTML文件
- 网络对战五子棋(web-gobang)项目
- NetApp存储方案及巡检命令
- 【NetApp】NetApp存储设备的CPU使用率分析
- 阿里云大学>【Python学习路线】Python语言基础自测考试 - 初级难度 | 包过关系列
- 魅族15android 8.0,Flyme 8体验版已上线:魅族15/16系列可直接升级
- VS2013安装SVN插件
- 概率论——马尔科夫链
- 内存继续涨价 LPDDR4/LPDDR4X内存标准升级