项目场景:

整合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的更多相关文章

  1. 自定义MVC框架之工具类-模型类

    截止目前已经改造了5个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 自定义MVC框架之工具类-图像处理 ...

  2. 自定义MVC框架之工具类-图像处理类

    截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( ...

  3. 自定义MVC框架之工具类-文件上传类

    截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...

  4. JVM 自定义类加载器在复杂类情况下的运行分析

    一.自定义类加载器在复杂类情况下的运行分析 1.使用之前创建的类加载器 public class MyTest16 extends ClassLoader{ private String classN ...

  5. 在Springmvc普通类@Autowired注入request为null解决方法

    在Springmvc普通类@Autowired注入request为null解决方法   在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...

  6. Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  7. IDEA自定义liveTemplates(方法模板、类模板)

    IDEA自定义liveTemplates(方法模板.类模板) 前言,搞这个模板有何意义? 降低大家写方法注释的成本,统一风格.有时候不是开发同学不爱写注释,而是没有合适的载体和空间. IDEA模板设置 ...

  8. springboot通过继承OncePerRequestFilter,在拦截器中@Autowired 为null问题

    springboot2版本以上环境 通过继承OncePerRequestFilter类,在重写doFilterInternal方法实现拦截的具体业务逻辑, 在实现的过程中,需要注入service方法, ...

  9. SpringSecurity自定义AuthenticationProvider和AuthenticationFilter

    AuthenticationProvider 默认实现:DaoAuthenticationProvider 授权方式提供者,判断授权有效性,用户有效性,在判断用户是否有效性,它依赖于UserDetai ...

  10. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

随机推荐

  1. Oracle 死锁与慢查询总结

    查看死锁 SELECT s.sid "会话ID", s.lockwait "等待锁", s.event "等待的资源/事件", -- 最近等 ...

  2. 题解:P10520 [XJTUPC2024] 榕树之心

    题意 给予你 \(x\) 和 \(y\),将 \(x,y\) 代入. 前面的一大堆都无用. 思路 将题目中的公式代入即可. 代码 #include<bits/stdc++.h> using ...

  3. CF1934B Yet Another Coin Problem 题解

    CF1934B Yet Another Coin Problem 题解 题意 目前有 \(5\) 种硬币,面值分别为 \(1,3,6,10,15\).给你一个数字 \(n\),求出可以凑出 \(n\) ...

  4. redis数据结构:RedisObject,SkipList,SortedSet

    1.RedisObject对象 redis中任何KV都会被封装为RedisObject对象,也叫做Redis对象 2.SkipList 跳表 元素按照升序排列存储,是有序的双向链表 节点可以有多个指针 ...

  5. 提高MQ可靠性

    提高可靠性通过以下四个方面: 生产者的可靠性(发送消息时丢失) 生产者发送消息时连接MQ失败 生产者发送消息到达MQ后未找到exchange 生产者发生消息到达MQ的exchange后,未找到合适的q ...

  6. Python+requests编写接口用例

    1.python编写接口用例需要依赖requests模块,我们先确认本地是否安装了requests模块,没有安装的先进行安装. pycharm工具查看已安装的模块:File->Setting-& ...

  7. 【Java-GUI】03 事件监听

    --1.监听机制案例 简答理解:操作驱动程序执行 完整的操作体系:事件源.事件.监听器.注册监听 案例: 点击OK按钮,让上方的输入框写入一段字符 package cn.dzz; import jav ...

  8. pygame游戏:python版本的贪吃蛇游戏 —— Python 贪吃蛇魔改大赛

    在网上找python版本的贪吃蛇游戏,看到一个Gitee Community / Python 贪吃蛇魔改大赛,感觉还不错,这里收藏下. 一等奖 1名 Snake Quest 蛇蛇闯关: jeffya ...

  9. 使用pycharm专业版(支持远程调试及运行)如何运行mpi的代码呢???(mpi4py的代码)

    问题如题: 请注意:这里pycharm专业版的远程调试及运行该如何设置不进行介绍. 由于mpi进程启动是需要执行mpiexec或mpirun命令的,然而在pycharm中我们只能远程调用Python命 ...

  10. MPI在Deep Learning的主流时代背景下除了传统计算领域外对DL的应用前景如何,MPI与NCCL的区别在哪???

    做分布式计算的基本上10年之前只听说过MPI,14年之前只听过hadoop的MapReduce,17年之前只听过TensorFlow. 那么这三个分布式计算软件或者说框架有什么区别呢???现在都是搞d ...