自定义 AuthenticationProvider ,UserDetailsService的实现类@Autowired 为null
项目场景:
整合spring security OAuth2自定义AuthenticationProvider 登录认证 签发token
问题描述:
在自定义 AuthenticationProvider 时 发现UserDetailsService 的实现类 UserService 一直注入不进去,为null
自定义 AuthenticationProvider 通过 username直接登录
package com.example.sso.provider;
import com.example.sso.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
@Slf4j
public class UsernameAuthenticationProvider implements AuthenticationProvider {
@Autowired
UserService userService;
public UsernameAuthenticationProvider() {
log.info("UsernameAuthenticationProvider loading......");
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = (String) authentication.getPrincipal();
UserDetails user = userService.loadUserByUsername(username);
if (null != user) {
return new UsernamePasswordAuthenticationToken(user, authentication.getCredentials(), user.getAuthorities());
}
return null;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
解决办法,看最后三组代码
重写
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(usernameAuthenticationProvider());
auth.userDetailsService(userService);
}
在该配置类,注入UserDetailsService的实现类UserService,并将其设置到AuthenticationManagerBuilder 中。
然后将自定义的 UsernameAuthenticationProvider 在此实例化。然后将其注入到 AuthenticationManagerBuilder 中,他是用来管理所有的provider的
package com.example.sso.config;
import com.example.sso.provider.UsernameAuthenticationProvider;
import com.example.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
//对外放开某些接口
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/login/getUser");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/**").authenticated().anyRequest().authenticated();//所有请求都需要验证
// .anyRequest().permitAll(); //其他所有请求都不需要验证
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
UserService userService;
@Bean
public UsernameAuthenticationProvider usernameAuthenticationProvider() {
return new UsernameAuthenticationProvider();
}
//添加自定义的provider
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(usernameAuthenticationProvider());
auth.userDetailsService(userService);
}
}
自定义 AuthenticationProvider ,UserDetailsService的实现类@Autowired 为null的更多相关文章
- 自定义MVC框架之工具类-模型类
截止目前已经改造了5个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 自定义MVC框架之工具类-图像处理 ...
- 自定义MVC框架之工具类-图像处理类
截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( ...
- 自定义MVC框架之工具类-文件上传类
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...
- JVM 自定义类加载器在复杂类情况下的运行分析
一.自定义类加载器在复杂类情况下的运行分析 1.使用之前创建的类加载器 public class MyTest16 extends ClassLoader{ private String classN ...
- 在Springmvc普通类@Autowired注入request为null解决方法
在Springmvc普通类@Autowired注入request为null解决方法 在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...
- Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- IDEA自定义liveTemplates(方法模板、类模板)
IDEA自定义liveTemplates(方法模板.类模板) 前言,搞这个模板有何意义? 降低大家写方法注释的成本,统一风格.有时候不是开发同学不爱写注释,而是没有合适的载体和空间. IDEA模板设置 ...
- springboot通过继承OncePerRequestFilter,在拦截器中@Autowired 为null问题
springboot2版本以上环境 通过继承OncePerRequestFilter类,在重写doFilterInternal方法实现拦截的具体业务逻辑, 在实现的过程中,需要注入service方法, ...
- SpringSecurity自定义AuthenticationProvider和AuthenticationFilter
AuthenticationProvider 默认实现:DaoAuthenticationProvider 授权方式提供者,判断授权有效性,用户有效性,在判断用户是否有效性,它依赖于UserDetai ...
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
随机推荐
- ABC354
A link 模拟整个过程即可. 点击查看代码 #include<bits/stdc++.h> #define int long long using namespace std; sig ...
- python+selenium基础之XPATH定位(第一篇)
世界上最远的距离大概就是明明看到一个页面元素站在那里,但是我却定位不到!! selenium定位元素的方法有很多种,像是通过id.name.class_name.tag_name.link_text等 ...
- 全网最适合入门的面向对象编程教程:24 类和对象的 Python 实现-异常的捕获与处理:try/except 语句、文件读写示例、Exception 引用
全网最适合入门的面向对象编程教程:24 类和对象的 Python 实现-异常的捕获与处理:try/except 语句.文件读写示例.Exception 引用 摘要: 本文主要介绍了在使用 Python ...
- OneFlow框架0.9.1dev版本,成功安装并运行
安装cuda和cudnn: (此步骤可以忽略,pip安装框架时会自动安装依赖的cuda和cudnn环境) conda install cudatoolkit==11.8.0 python3 -m pi ...
- 外文论文同行评审平台——PubPeer——论文打假平台
参考: https://baijiahao.baidu.com/s?id=1757051752090030001&wfr=spider&for=pc ================= ...
- openAI的仿真环境Gym Retro的Python API接口(续1)—— 游戏过程记录及回放
如题,本文主要介绍仿真环境Gym Retro的Python API接口 . 官网地址: https://retro.readthedocs.io/en/latest/python.html 本文环境配 ...
- 读论文《IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures》——(续)实验部分
论文地址: https://arxiv.org/pdf/1802.01561v2.pdf 论文<IMPALA: Scalable Distributed Deep-RL with Importa ...
- maven项目打包时排除依赖包
1.背景 为了快速上传jar包到服务器,很多时候我们需要把依赖包单独独立出来,避免每次修改都传依赖包 2.实现方式 maven的pom文件,没有独立依赖包时配置如下: <build> &l ...
- 简化数据流:Apache SeaTunnel实现多表同步的高效指南
Apache SeaTunnel除了单表之间的数据同步之外,也支持单表同步到多表,多表同步到单表,以及多表同步到多表,下面简单举例说明如何实现这些功能. 单表 to 单表 一个source,一个sin ...
- Docker 之 RabbitMQ安装教程 基于腾讯云
由于需要学习RabbitMQ(主要是项目启动了mq本地却没有服务控制台老是在连mq,致使我热部署都不知道到底成功没),所以才有了这个教程,下面介绍RabbitMQ准备工作 下载所需环境 yum -y ...